Addhandler/Removehandler

J

Jim

Hi all,
We have developed some forms that dynamically build the
controls and then need to add event handlers to them.
Does anyone know the cumulative effect of adding handlers
and not removing them? I have done Removehandler calls
when the handler doesn't exist so I assume that the
handlers are not maintained as a collection.

Jim
 
H

Herfried K. Wagner [MVP]

Hello,

Jim said:
We have developed some forms that dynamically build the
controls and then need to add event handlers to them.
Does anyone know the cumulative effect of adding handlers
and not removing them? I have done Removehandler calls
when the handler doesn't exist so I assume that the
handlers are not maintained as a collection.

Handlers may fire twice. I suggest you read the chapter about events in
the documentation:

Events and Delegates (Visual Basic Language Concepts)
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconeventsdelegatesinheritance.asp
 
J

Jim

Hi guys,
Thanks for the replies. Yes that was my question really,
whether anyone knew for sure that the handlers would in
fact be duplicated or whether the event queue would
detect that a hander was already registered for contol X
and method Y. The documentation is not detailed enough
to make that determination so I guess I just have to play
with the code to see for sure.

jim
 
Y

Ying-Shen Yu[MS]

Hi Jim,
Just as our MVP and other community users said, you will fire the
event twice.
The event in .NET Framework is implemented by the MultiDelegate class,
which uses an linked list to save event handlers.It is not like an
Collection and won't check for duplicate handlers. for more information
,please refer to
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemmulticastdelegatecla
sstopic.htm
If you have any further questions , please be free to let me know!
Thanks!

Kind regards,

Ying-Shen Yu [MS]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| Content-Class: urn:content-classes:message
| From: "Jim" <[email protected]>
| Sender: "Jim" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Addhandler/Removehandler
| Date: Tue, 9 Sep 2003 14:47:50 -0700
| Lines: 42
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: quoted-printable
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcN3HAQj7olZy3vnQp2jDEudbVgEPg==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:51984
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi guys,
| Thanks for the replies. Yes that was my question really,
| whether anyone knew for sure that the handlers would in
| fact be duplicated or whether the event queue would
| detect that a hander was already registered for contol X
| and method Y. The documentation is not detailed enough
| to make that determination so I guess I just have to play
| with the code to see for sure.
| jim
| >-----Original Message-----
| >Hello,
| >
| >> We have developed some forms that dynamically build the
| >> controls and then need to add event handlers to them.
| >> Does anyone know the cumulative effect of adding
| handlers
| >> and not removing them? I have done Removehandler calls
| >> when the handler doesn't exist so I assume that the
| >> handlers are not maintained as a collection.
| >
| >Handlers may fire twice. I suggest you read the chapter
| about events in
| >the documentation:
| >
| >Events and Delegates (Visual Basic Language Concepts)
| >http://msdn.microsoft.com/library/en-
| us/vbcn7/html/vaconeventsdelegatesinheritance.asp
| >
| >--
| >Herfried K. Wagner
| >MVP · VB Classic, VB.NET
| >http://www.mvps.org/dotnet
| >
| >
| >.
| >
|
 
S

Stuart Wells

Also is you do not unregister the events - even though the
registered component may not be used anymore the event
register will still exist, preventing the component from
being collected and worse it still responds to events!

Stuart
 

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