473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AddHandler/RemoveHandler Clarification (Dynamically created controls)

MLS
The documentation on dynamic handlers comes across as abiguous.
Perhaps somebody could help set me straight?

I have a situation where I need to dynamically create several
usercontrols of the same type within a form. This usercontrol has some
events associated with it.

For example:

Private Sub NewThingy()
'TODO clean things up better than this (removehandler and
all...)
tabMain.SelectedTab.Controls.Clear()

Dim oMyControl As New MyControl
tabMain.SelectedTab.Controls.Add(oMyControl)

' ... stuff

AddHandler oMyControl.MyEventName, AddressOf MyEventHandler
End Sub

Private Sub MyEventHandler(Parameter as string)
' ... stuff
End Sub

Now, I create four tabs, then call NewThingy within each of them.

So, I now have four instances of MyControl, all pointing at
MyEventHandler.

If one of my instances raises its event, does it fire four times due
to the four AddHandler statements? Or, would it fire only once due to
the fact that oMyControl.MyEventName is only for the one instance of
MyControl?

If I do a RemoveHandler, am I just pulling the first MyEventName off
the stack, am I removing the handler for a specific object instance,
or am I removing all instances of handlers pointing to MyEventName?

I want to be able to create and clean up these handlers properly with
these dynamically created and destroyed controls. Any help would be
appreciated.

Thanks
- mls
Nov 20 '05 #1
2 3273

"MLS" <ml*@bitstream.net> wrote in message
news:fd**************************@posting.google.c om...
The documentation on dynamic handlers comes across as abiguous.
Perhaps somebody could help set me straight?

I have a situation where I need to dynamically create several
usercontrols of the same type within a form. This usercontrol has some
events associated with it.

For example:

Private Sub NewThingy()
'TODO clean things up better than this (removehandler and
all...)
tabMain.SelectedTab.Controls.Clear()

Dim oMyControl As New MyControl
tabMain.SelectedTab.Controls.Add(oMyControl)

' ... stuff

AddHandler oMyControl.MyEventName, AddressOf MyEventHandler
End Sub

Private Sub MyEventHandler(Parameter as string)
' ... stuff
End Sub

Now, I create four tabs, then call NewThingy within each of them.

So, I now have four instances of MyControl, all pointing at
MyEventHandler.

If one of my instances raises its event, does it fire four times due
to the four AddHandler statements? Or, would it fire only once due to
the fact that oMyControl.MyEventName is only for the one instance of
MyControl?
It fires once. It just happens that each of the four controls fires the same
method when it needs to raise the event. If you need to know what control
invoked the event, you might consider making your event signature look more like
the standard .NET events. If you look at most event procedures, you will see
something like this:

Private Sub EventHandlerOfSomeKind(ByVal sender As Object, ByVal e As EventArgs)
'
End Sub

The reason they do this is because the object that raised the event passes it
self in the sender parameter:

RaiseEvent EventHandler(Me, E)
If I do a RemoveHandler, am I just pulling the first MyEventName off
the stack, am I removing the handler for a specific object instance,
or am I removing all instances of handlers pointing to MyEventName?
Your basically just removing it for that particular control. You will want to
call RemoveHandler on all controls tied to a particular procedure.
I want to be able to create and clean up these handlers properly with
these dynamically created and destroyed controls. Any help would be
appreciated.


The simple answer is that you should pretty much make sure that you call
RemoveHandler for every AddHandler...

Tom Shelton

Nov 20 '05 #2
MLS
"Tom Shelton" <to*@mtogden.com> wrote in
news:eN**************@TK2MSFTNGP11.phx.gbl:

It fires once. It just happens that each of the four controls fires
the same method when it needs to raise the event.

...

Your basically just removing it for that particular control. You will
want to call RemoveHandler on all controls tied to a particular
procedure.

...

Tom Shelton


Thanks Tom, that's how I believed it should work, but was not clear.
Nov 20 '05 #3

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

Similar topics

0
1552
by: AJP | last post by:
I have a child window which does the following after a button is pressed: Response.Write("<script>window.opener.__doPostBack('','');</script>"); but I get the following uncaught exception upon...
1
3007
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
2556
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
5
3496
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator;...
2
1519
by: Nathan Sokalski | last post by:
I have several Button controls that I created dynamically that I need to handle the Click events for. How do I specify the event handler for dynamically created controls? Thanks. -- Nathan...
6
1783
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
1
1858
by: Grega | last post by:
Hi all, I am trying to create a VB smart device application. I create few buttons dynamically on form_load... something like this: Dim WithEvents BtnOperate As New...
2
2218
by: Dica | last post by:
i've got a script that creates a new tablerow + tablecell and appends that to a non dynamically created table control. a new row is added for each recordset returned from my sql statement. within...
2
1382
by: Dwight Johnson | last post by:
I am building a website in VS2005. I basically have one page, default.aspx, which contains placeholder objects into which I add various controls that are dynamically created. I have a dropdownlist...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.