PC Review


Reply
Thread Tools Rate Thread

AddHandler etc.

 
 
eBob.com
Guest
Posts: n/a
 
      17th Mar 2005


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





 
Reply With Quote
 
 
 
 
VJ
Guest
Posts: n/a
 
      17th Mar 2005
See Answers below..

"eBob.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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
>
>
>
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      17th Mar 2005
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


 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      17th Mar 2005
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


 
Reply With Quote
 
VJ
Guest
Posts: n/a
 
      17th Mar 2005
Yea thanks.. that was typo... And you have a good point, thanks I will keep
a note of it.

VJ

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
eBob.com
Guest
Posts: n/a
 
      18th Mar 2005

Gentlemen,

Thank you for your help.

Bob

On Wed, 16 Mar 2005 21:47:10 -0600, "VJ" <(E-Mail Removed)> wrote:

>See Answers below..
>
>"eBob.com" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>>
>>
>> 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
>>
>>
>>
>>
>>

>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why use AddHandler? Jim Hubbard Microsoft Dot NET 7 15th Dec 2005 03:14 AM
Addhandler help UGH Microsoft ASP .NET 7 30th Nov 2004 02:24 PM
Using AddHandler Rick Palmer Microsoft VB .NET 10 6th Oct 2004 10:15 PM
Addhandler...... Supra Microsoft Dot NET 0 4th Aug 2004 04:22 PM
AddHandler Micke Palm Microsoft ASP .NET 0 12th May 2004 09:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 AM.