View Single Post
Old 05-08-2006, 10:24 AM   #2 (permalink)
tvqpriqphh
Registered User
 
Join Date: May 2006
Posts: 4
tvqpriqphh is on a distinguished road
Thumbs down

The code sample doesn't describe a control array, rather "programmatic addressing". This code snippet does exactly the same thing (in a less rediculous fashion):

HTML Code:
select case txtwhotext.text case "1" textbox1.text = "1" case "2" textbox2.text = "2" case "3" textbox3.text = "3" case "4" textbox4.text = "4" end select
if you want an array of controls, try something like this:

HTML Code:
' this is a class/form instance variable ' use nothing as a place holder for element zero in the zero based array dim ca as control() = new control() {nothing, textbox1, textbox2, _ textbox3, textbox4} sub dosomething() ' get the user specified index dim index as integer = cint(textwhotext.text) ' put the user specified message into that textbox ca(index).text = textwhotext.text end sub
note that this really only gets you halfway home -- you now have a way to translate an integer index into a control object reference -- how do you convert a control object reference into an integer index?

HTML Code:
function getindex(c as control) as integer for i as integer = 1 to ca.length-1 if c is ca(i) then return i end if next throw new applicationexception("control reference not found") end function
now we can have "control array" based events like this (having to manually bind events makes maintaining this code a bit ugly)
HTML Code:
sub click_handler(sender as object, ea as eventargs) handles _ textbox1.click, textbox2.click, textbox3.click, textbox4.click dim i as integer = getindex(sender) ca(i).text = "you clicked me" end sub
note that .net sort of obsoletes the need for control arrays, because you could just have easily defined click_handler as follows:

HTML Code:
sub load addhandler textbox1.click, addressof (click_handler) addhandler textbox2.click, addressof (click_handler) addhandler textbox3.click, addressof (click_handler) addhandler textbox4.click, addressof (click_handler) end sub sub click_handler(sender as object, ea as eventargs) ctype(sender, control).text = "you clicked me" end sub
__________________
tvqpriqphh is offline   Reply With Quote