By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

AddHandler etc.

P: n/a


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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
VJ
See Answers below..

"eBob.com" <eB******@totallyfakeisp.com> wrote in message
news:tj********************************@4ax.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.
- 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.

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


Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
VJ
Yea thanks.. that was typo... And you have a good point, thanks I will keep
a note of it.

VJ

"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oz**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #5

P: n/a

Gentlemen,

Thank you for your help.

Bob

On Wed, 16 Mar 2005 21:47:10 -0600, "VJ" <vi********@yahoo.com> wrote:
See Answers below..

"eBob.com" <eB******@totallyfakeisp.com> wrote in message
news:tj********************************@4ax.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.


- 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.

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



Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.