WithEvents and Array

DreamKid
02-04-2004, 11:14 AM
I know that "WithEvents x(10) As New Button" is not possible.
How can I input functions into the arrays of buttons that been generate in coding.
Is it possible as I know that VB.Net doesn't support control array. :confused:

OnErr0r
02-04-2004, 11:29 AM
AddHandler will programmatically add controls to a given event. It's like adding "Handles Controlname.Event" to the end of an event at runtime. Note that the events are additive, that is they do not replace previous events.

DreamKid
02-04-2004, 11:36 AM
Can you give me some examples?
I just started to learn VB.Net.
Thanks.

Iceplug
02-04-2004, 11:51 AM
To use AddHandler:

AddHandler MyControl.MyEvent, AddressOf MyControl_MyEvent
'For example:
AddHandler btn2.Click, AddressOf btn_Click
'or, as an alternative to AddHandler
Private Sub Btn2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn2.Click


btn2.lick lol

OnErr0r
02-04-2004, 12:39 PM
Iceplug: *hehe*

DreamKid: check this out too:
http://www.visualbasicforum.com/showthread.php?t=133197

DreamKid
02-04-2004, 12:51 PM
Thanks guys. :)

Is it possible to add functions into an array?
For example:

Private Sub x_Click(ByVal sender As Object, ByVal e As EventArgs) Handles x.Click <-- Non-Array

Private Sub x(1)_Click(ByVal sender As Object, ByVal e As EventArgs) Handles x(1).Click <-- Array
(Any possible way to do this?)

OnErr0r
02-04-2004, 12:55 PM
Thanks guys. :)

Is it possible to add functions into an array?
For example:

Private Sub x_Click(ByVal sender As Object, ByVal e As EventArgs) Handles x.Click <-- Non-Array

Private Sub x(1)_Click(ByVal sender As Object, ByVal e As EventArgs) Handles x(1).Click <-- Array
(Any possible way to do this?)

Not at design time, no. But that is in effect what my code does at runtime. I imagine the designer will eventually handle control arrays, due to popular demand.

DreamKid
02-04-2004, 01:58 PM
After adding a Handler to the array, how can I determine which button that I had click on?

AddHandler cmd(i).TextChanged, New System.EventHandler(AddressOf cmd_Click)

Private Sub cmd_Click(ByVal sender As Object, ByVal e As EventArgs)

Msgbox i

End Sub

If there are 3 buttons, I want different function for each button. Is that possible?

Optikal
02-04-2004, 02:48 PM
sure, just create 3 different functions and use AddHandler to link each one up with your buttons.

Or you can check the sender parameter to see which button caused the event.

As for arrays of functions, you can accomplish this using delegates, although I can't see why you would need this for your problem. More information on delegates can be found here: http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsdelegates.asp

DreamKid
02-04-2004, 08:35 PM
Thanks!

I too got some help from here:
http://www.informit.com/isapi/guide~dotnet/seq_id~142/guide/content.asp

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum