Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
There is Mdi a form created on a template. There is a directory form, and the adding form. On the adding form there is an event declared as shared, which rises on button click, and on the directory form there is a handler of this event. I subscribe for it in the constructor of a directory form. On closing of a directory form i remove handler.

Why handling sub on a directory works so much time, how many it has been opened? I.e. 3 times I open and close, handling sub exucutes 3 times.

adding and directory forms inherited from abstract classes.

how can i get number of active handlers? or how should i convert EventHandlerList to obtain count method? i tried to convert into HashTable but error occurs.

Please Help!
Posted
Comments
OriginalGriff 20-Mar-11 5:36am    
Answer updated

Because when you subscribe to an event handler it stays subscribed until the object is destroyed or the handler is removed. It's like macro key in an editor: you assign a macro to the key once, and every time you use the key, the macro gets executed.
So if you set one handler in your constructor and remove it when you close the form, then every time that the event is raised, the handler will be called.


"thanks for your time. But can you tell me what should i do to avoid handler double calling? as you said "Because when you subscribe to an event handler it stays subscribed until the object is destroyed or the handler is removed" in my case removehandler doesn't work ?"


Difficult to tell: what is your code for removing the handler? Why do you say "it doesn't work"? What does happen?
 
Share this answer
 
v2
Comments
Indivara 20-Mar-11 0:08am    
Member 7653505119:
"thanks for your time. But can you tell me what should i do to avoid handler double calling? as you said "Because when you subscribe to an event handler it stays subscribed until the object is destroyed or the handler is removed" in my case removehandler doesn't work ?"
My answer is simple: don't use MDI!

There are now situations where this extremely inconvenient UI style helps. Is it totally discouraged by practically anyone and considered to be a Microsoft mistake in first place. By using MDI you can only scare off your customers. Prefer design based on just one main window. As a next level of hierarchy you can use tabbing interface and more.

—SA
 
Share this answer
 
Comments
Albin Abel 20-Mar-11 23:09pm    
Agreed. Sometime back when I use the .Net profiler the MDI parent has a reference to the child forms, even after the child has disposed. Not sure why. Anyhow as you said it is not a smooth way. My 5
Sergey Alexandrovich Kryukov 20-Mar-11 23:19pm    
Not only this is ugly.
Thank you Albin.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900