Array or collection of withevents?

R

Rob Nicholson

A thought - has VB.NET been extended to be able to handle arrays or
collections with events? A problem with VB6 was that you could only declare:

Private WithEvents MyControl As MyControl

Private Sub MyControl_Click()
End Sub

which could track one control on the form. If you happened to have code that
add N controls at runtime, you had to create a wrapper class to capture the
event, i.e. one object per control with the class containing the WithEvent
declaration. I'd expect something like:

Private WithEvents MyControls() As MyControl

Private Sub MyControls_Click(Index As Integer)
End Sub

Hmm, I'm guessing not as it's not ringing any bells.

Cheers, Rob.
 
S

Samuel R. Neff

No, you get compiler error "with events variable does not raise
events."

It is possible to create a custom collection that listens to and
rebroadcasts events from any contained object, but that's not built
in.

Sam


A thought - has VB.NET been extended to be able to handle arrays or
collections with events? A problem with VB6 was that you could only declare:

Private WithEvents MyControl As MyControl

Private Sub MyControl_Click()
End Sub

which could track one control on the form. If you happened to have code that
add N controls at runtime, you had to create a wrapper class to capture the
event, i.e. one object per control with the class containing the WithEvent
declaration. I'd expect something like:

Private WithEvents MyControls() As MyControl

Private Sub MyControls_Click(Index As Integer)
End Sub

Hmm, I'm guessing not as it's not ringing any bells.

Cheers, Rob.

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
C

Chris Dunaway

You can do what you want using the AddHandler statement to wire up the
event at runtime.

Private MyControls() As MyControl

'Populate array here

'wire up the events
For Each mc As MyControl in MyControls
AddHandler mc.Click, AddressOf MyControlClickHandler
Next
 
C

Cor Ligthert

Rob,

You can add to any control a lot of events which is only one sub.

There is no problem when you say (when the passing values are the same)
\\\
Addhandler mybutton.click, addressof MyClickEvent
Addhanlder mytextbox.click, addressof MyClickEvent
Private MyClickEvent(ByVal sender As System.Object, ByVal e As
System.EventArgs)
if directcast(sender, control).name = "mybutton" then
'do something because of the click
end if
End sub
///

I hope this was what you where after?

Cor
 
C

Cor Ligthert

Hi Chris,

Together are our messages a nice sample :)

I could not come on that loop.

I go look to Barcelona<->Chelsea.
This proofs it was enough for today.

:)

Cor
 
R

Rob Nicholson

It is possible to create a custom collection that listens to and
rebroadcasts events from any contained object, but that's not built
in.

That was in effect what you did in VB6.

Cheers, Rob.
 
R

Rob Nicholson

For Each mc As MyControl in MyControls
AddHandler mc.Click, AddressOf MyControlClickHandler
Next

Ahh thanks, useful to know about AddHandler - although that's sounds more a
meld of the CLR/framework than specific syntax changes to the VB language.
To be picky :)

Cheers, Rob.
 
R

Rob Nicholson

I hope this was what you where after?

Not exactly but it's a perfectly workable alternative way of handling the
same problem.

Cheers, Rob.
 

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