473,378 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Array or collection of withevents?

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.
Nov 21 '05 #1
7 8103

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"
<ro***********@nospam-unforgettable.com> 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.
Nov 21 '05 #2
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

Nov 21 '05 #3
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
Nov 21 '05 #4
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
Nov 21 '05 #5
> 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.
Nov 21 '05 #6
> 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.
Nov 21 '05 #7
> 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.
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: vivek | last post by:
Hello Every body, I am tring to make an array of server controls with array indes can change dynamically. But I was not able to get the refrence of the control outside the scope in which they...
3
by: Terry Olsen | last post by:
I want to have an array of class objects that raise an event when a condition is true. Like so... ------------------------------------------------------- Public Class ClientHandler Public...
1
by: GL | last post by:
I am trying to develop a prgram that randomly selects numbers for an addition program, then when the check answer button is pressed it colors the answer text field either green or red depending if...
4
by: c676228 | last post by:
Hi all, In the second web form, I have the following code: (The peopleinfo is a user control which has name and ssn infomation, each field in this user control I have set enableviewstate=true). I...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
18
by: Jerry Boone | last post by:
I'm looking for a way to fire a public sub/function when a form is opened and closed - without using form level events. I have already done some extensive form work with instancing, looping...
3
by: RallyDSM | last post by:
Pre STory - I've had a lot of problems with this program, and I just added the last part of it (the add item code) and now an older part of the program crashes. Public Structure Stocks Public...
1
by: planet.creativa | last post by:
There are two classes 1) Key - contains some public variables, one method "Reset" and one event "valuesReset" which is raised by Reset method. 2) Keys - this class is collection of objects of...
4
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I googled some and found that I can no longer - dim withevents myclassname() I also understand that I can use addhandler... But... what if I do not know the amount of classes that will be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.