AddHandler etc.

E

eBob.com

I've used AddHandler for a UNIQUE control added to a panel and it
seemed to work. But now I want to add a bunch of controls to a panel
and use only one event handler subroutine. And I am completely lost.

Below is the essence of the code adding checkbox controls to a panel.
(Corresponding to each checkbox is another checkbox and a label.)

Do I have to use AddHandler in the loop? Or is there someway outside
of a loop to say that a particular sub is the event handler for all
controls in an array of controls? The syntax of my AddHandler is
wrong but I cannot figure out how to fix it.

I have to be able to figure out in the event handler code WHICH
checkbox was checked. So I'd like to get the index value of the
control passed as an argument to the event handler sub. But that's
another thing which I can't figure out.

When I pnlAttrs.Controls.Clear() will that undo the AddHandler or must
I use RemoveEventHandler? And what would the syntax of the
RemoveEventHandler be?

Here's the code ...

For i As Integer = 0 To x
chkbxSel(i) = New CheckBox
pnlAttrs.Controls.Add(chkbxSel(i))
AddHandler chkbxSel(i).Click, AddressOf chkbxSelI_Click '< ??
Next


Private Sub chkbxSelI_Click(ByVal i As Integer)
MsgBox("bingo for number " & i.ToString)
End Sub


Any and all help will be appreciated.

Thanks, Bob
 
V

VJ

See Answers below..

eBob.com said:
I've used AddHandler for a UNIQUE control added to a panel and it
seemed to work. But now I want to add a bunch of controls to a panel
and use only one event handler subroutine. And I am completely lost.

- Yes you can use one routinue.. it will work, you say Addressof
Routine1_Click and attach it to all CheckBox's click event..
Below is the essence of the code adding checkbox controls to a panel.
(Corresponding to each checkbox is another checkbox and a label.)

Do I have to use AddHandler in the loop? Or is there someway outside
of a loop to say that a particular sub is the event handler for all
controls in an array of controls? The syntax of my AddHandler is
wrong but I cannot figure out how to fix it.

- You can have it in the Loop.. perfectly ok.. , regarding your syntax i am
not sure why you are using array's.. the below do the trick

For i As Integer = 0 To x
chkbxSel = New CheckBox
pnlAttrs.Controls.Add(chkbxSel)
AddHandler chkbxSel(i).Click, AddressOf chkbxSelI_Click
Next

Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("bingo for number " & i.ToString)
End Sub
I have to be able to figure out in the event handler code WHICH
checkbox was checked. So I'd like to get the index value of the
control passed as an argument to the event handler sub. But that's
another thing which I can't figure out.

Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("bingo for number " & i.ToString)
CheckBox chk1 = Ctype(sender,Checkbox)
'Chk1 will give you the CheckBox that was clicked.. you can use the
name to identify the box, or something in
'Check box tag to identify the box... I would prefer using name
End Sub
When I pnlAttrs.Controls.Clear() will that undo the AddHandler or must
I use RemoveEventHandler? And what would the syntax of the
RemoveEventHandler be?

- Yes... u don't need to use RemoveHandler.
 
C

Cor Ligthert

VJ,
- You can have it in the Loop.. perfectly ok.. , regarding your syntax i
am not sure why you are using array's.. the below do the trick

Was my thought as well, however I was first looking at your sample and than
saw this text.
For i As Integer = 0 To x
chkbxSel = New CheckBox
pnlAttrs.Controls.Add(chkbxSel)
AddHandler chkbxSel(i).Click, AddressOf chkbxSelI_Click
Next
AddHandler chkbxSel.Click, AddressOf chkbxSelI_Click

I assume just a typo, however to make it not to difficult for the OP.

Cor
 
C

Cor Ligthert

EBob,

See bellow
For i As Integer = 0 To x
chkbxSel(i) = New CheckBox
pnlAttrs.Controls.Add(chkbxSel(i))
AddHandler chkbxSel(i).Click, AddressOf chkbxSelI_Click '< ??
Next


Private Sub chkbxSelI_Click(ByVal i As Integer)
MsgBox("bingo for number " & i.ToString)
End Sub
I think that this will only go when it would be

For i As Integer = 0 To x
chkbxSel = New UNIQUEControl
pnlAttrs.Controls.Add(chkbxSel)
AddHandler chkbxSel.Click, AddressOf chkbxSelI_Click
Next

The original CheckBox has other parameters in his Click Event than your
UNIQUEControl so what you are doing now will not be accepted.

(You can use the array of course as well when you want afterwards use that
to set the properties of those checkboxes, however you can set those much
nicer in this loop and than you do not need the array as VJ already wrote.)

I hope this give the idea?

Cor
 
V

VJ

Yea thanks.. that was typo... And you have a good point, thanks I will keep
a note of it.

VJ
 
E

eBob.com

Gentlemen,

Thank you for your help.

Bob

See Answers below..



- Yes you can use one routinue.. it will work, you say Addressof
Routine1_Click and attach it to all CheckBox's click event..


- You can have it in the Loop.. perfectly ok.. , regarding your syntax i am
not sure why you are using array's.. the below do the trick

For i As Integer = 0 To x
chkbxSel = New CheckBox
pnlAttrs.Controls.Add(chkbxSel)
AddHandler chkbxSel(i).Click, AddressOf chkbxSelI_Click
Next

Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("bingo for number " & i.ToString)
End Sub


Private Sub chkbxSelI_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("bingo for number " & i.ToString)
CheckBox chk1 = Ctype(sender,Checkbox)
'Chk1 will give you the CheckBox that was clicked.. you can use the
name to identify the box, or something in
'Check box tag to identify the box... I would prefer using name
End Sub


- Yes... u don't need to use RemoveHandler.
 

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

Top