Newbie: Event with Control Array

B

bz

Hi,

There is no control array. So, if I have a hundred buttons, do I list all
100 buttons after the Handle keyword?

Is it how it is done in VB.NET?

Thanks!

Something like:

Sub My_Button_Event_Handler(sender, your_event) Handle button1.click,
button2.click, button3.click, ...buttonN-1.click, button<infinty>.click




--
There is no answer.
There has not been an answer.
There will not be an answer.
That IS the answer!
And I am screwed.
Deadline was due yesterday.

There is no point to life.
THAT IS THE POINT.
And we are screwed.
We will run out of oil soon.

http://spaces.msn.com/bzDaCat
 
R

RobinS

You have a hundred buttons that all do the same thing?

You're right, there is no control array. You can
set up event handlers for each button and have them
run the same routine.

AddHandler myButton.Click, AddressOf RunThisRoutine

You can write a routine that does this in a loop.
This would be called from your Form Load event:

'This recurses, so it picks up controls inside panels, etc.
Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
'Debug.Print(ctrl.Name.ToString)
If TypeOf ctrl Is Button Then
AddHandler ctrl.Click, AddressOf RunThisRoutine
End If
'if control has children, call this function recursively
If ctrl.HasChildren Then
AddEventHandlers(ctrl)
End If
Next
End Sub

Robin S.
 
R

RobinS

If you have panels on your form, and you have controls inside
the panels, they are children of the panel. The first
"level" of AddEventHandlers will pick up the panels, but
the children are invisible to the first "level". So you
have to check each control to see if it has controls inside
it, and if so, call the routine recursively to handle those
controls. If those controls have more controls inside them,
it will call it recursively again. Eventually, it will hit
controls without children, and it will work itself
back up the tree.

This also works if you have a user-control that has controls
inside it.

You may not have that sort of thing now, but one day you will,
and it's best to write the code to handle what *could* happen
rather than what *should* happen.

Put some panels on your form, and put some controls inside it,
and then add a call to AddEventHandlers to your Form_Load.
Uncomment the line in AddEventHandlers that prints out the
name of the current control, and step through it. You'll
see what I'm talking about.

Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

newbie: RadioButton 6
VB.NET and MDAC+JET 10
newbie - dataset 1
newbie: how to use the CheckListBox 4
VB Controls In VB2005 9
Power Supply 20
Compact and Repair 2
what brand of hd 13

Top