PC Review


Reply
Thread Tools Rate Thread

Array or collection of withevents?

 
 
Rob Nicholson
Guest
Posts: n/a
 
      8th Mar 2005
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.


 
Reply With Quote
 
 
 
 
Samuel R. Neff
Guest
Posts: n/a
 
      8th Mar 2005

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


On Tue, 8 Mar 2005 18:39:04 -0000, "Rob Nicholson"
<(E-Mail Removed)> wrote:

>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.
 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      8th Mar 2005
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

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


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


 
Reply With Quote
 
Rob Nicholson
Guest
Posts: n/a
 
      9th Mar 2005
> 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.


 
Reply With Quote
 
Rob Nicholson
Guest
Posts: n/a
 
      9th Mar 2005
> 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.


 
Reply With Quote
 
Rob Nicholson
Guest
Posts: n/a
 
      9th Mar 2005
> 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.


 
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
Dim withevents - array of classes Guy Cohen Microsoft VB .NET 5 5th Nov 2010 05:12 PM
Array of Buttons WithEvents =?Utf-8?B?Q3JhaWc=?= Microsoft VB .NET 3 21st May 2006 10:23 PM
FileSystemWatcher array WithEvents DiGa Microsoft VB .NET 2 15th Jul 2005 02:22 PM
how to WithEvents on a local array of Radiobuttons? Kai Zhang Microsoft VB .NET 1 12th Dec 2004 09:37 PM
I need an array of WithEvents Classes... Terry Olsen Microsoft VB .NET 3 23rd Nov 2004 08:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.