Home All Groups Group Topic Archive Search About

Newbie: Event with Control Array

Author
9 Nov 2006 10:27 PM
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

Author
9 Nov 2006 11:11 PM
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.

Show quoteHide quote
"bz" <nospam@yahoo.com> wrote in message
news:%23iBkI8EBHHA.3396@TK2MSFTNGP02.phx.gbl...
> 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
>
Author
10 Nov 2006 6:49 PM
bz
Thanks.  But what is the purpose of this line?
Show quoteHide quote
>        'if control has children, call this function recursively
>        If ctrl.HasChildren Then
>            AddEventHandlers(ctrl)
>        End If



"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:DPidnY6T1fUgKM7YnZ2dnUVZ_v2dnZ2d@comcast.com...
> 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.
>
> "bz" <nospam@yahoo.com> wrote in message
> news:%23iBkI8EBHHA.3396@TK2MSFTNGP02.phx.gbl...
>> 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
>>
>
>
Author
10 Nov 2006 10:43 PM
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.

Show quoteHide quote
"bz" <nospam@yahoo.com> wrote in message
news:eg3RDnPBHHA.4256@TK2MSFTNGP04.phx.gbl...
> Thanks.  But what is the purpose of this line?
>>        'if control has children, call this function recursively
>>        If ctrl.HasChildren Then
>>            AddEventHandlers(ctrl)
>>        End If
>
>
>
> "RobinS" <RobinS@NoSpam.yah.none> wrote in message
> news:DPidnY6T1fUgKM7YnZ2dnUVZ_v2dnZ2d@comcast.com...
>> 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.
>>
>> "bz" <nospam@yahoo.com> wrote in message
>> news:%23iBkI8EBHHA.3396@TK2MSFTNGP02.phx.gbl...
>>> 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
>>>
>>
>>
>
>