Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have an array of 4-25 objects (normally) want them to raise an event at a specific time passing the information of which one they are. Say for example I just want to send another class a string containing some data.

 

VS says I cannot declare a variable WithEvents if it's part of an Array.

 

Is there anyther way to get around this?

  • Leaders
Posted

Look up AddHandler in the Help. The Handles clause (in conjuntion with WithEvents) in VB essentially breaks down to the same thing as using AddHandler, it is just easier on the eyes.

 

When you instantiate each object in the array, use the AddHandler statement to add the event handlers.

 

Dim btnButtonsGalore As Button() = New Button(16) {}

Sub Populate
For i As Integer = 0 To btnButtonsGalore.Length() - 1
	btnButtonsGalore(i) = New Button
	AddHandler btnButtonsGalore(i).Click, AddressOf btnButtonsGalore_Click
Next i
End Sub
Sub btnButtonsGalore_Click(sender As Object, e As EventArgs)
'...
End Sub

[sIGPIC]e[/sIGPIC]
Posted

ok I knew there was some way to delegate this but it's been so long I was just thinking of raising events within the class itself.

 

Let me question really quickly...

 

AddHandler btnButtonsGalore(i).Click, AddressOf btnButtonsGalore_Click

 

AddHandler is grabbing the event and adding it to the procedure btnButtonsGalore_Click.

 

So this way you don't need to "hardcode" anything in advance, it just sends all those buttons' Click Event to that one procedure.

 

That would be exactly what I was looking for.

 

Thank you :D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...