Hi Joe,
When the form is shown for the second time, is it a new instance that is
different from the previous instance? Is so, the UserControl on the new
form is a new instance as well. Then the NameChanged event of the new
usercontrol will only hold a reference to the event handler in the new
form, unless the NameChanged event is static.
I performed a test based on your description but didn't produce the
problem. The following is the steps of my test.
1. Create a WindForms application project. Add a UserControl and a form
called From2 in the project.
2. Add a static event to the UserControl. Add a button on the UserControl
to trigger this event.
3. Build the project. Add an instance of the UserControl on Form2 and
subscribe the event of the UserControl in the Form2's constructor.
4. Add a button on Form1. In the button's Click event handler, create a new
instance of Form2 and show it.
Run the program. When I click the button on the Form1 for the first time, a
Form2 appears. I click the button on the Form2 and the event handling
method in the Form2 is executed. Close the Form2. When I click the button
on the Form1 for the second time, a new Form2 instance appears. I click the
button on the new Form2 and the event handling method is executed twice,
which means that the event delegate of the UserControl still holds the
reference to the event handling method in the first Form2 instance. I don't
encounter any exception.
Actually, there's another case that the event receiver is disposed before
the event sender. In this case, when the event is fired, the event delegate
still holds the reference to the event handling method in the event
receiver. For example, there're two forms called Form1 and Form2. Form1 has
an event and Form2 subscribe this event. When the two forms are shown, all
works fine. Now close the Form2 and then trigger the event of Form1. The
event handling method in Form2 is still be executed without any exception.
As for the first case, we should unsubscribe the event of the UserControl
in the form when the form is closed.
As for the second case, we should unsubscribe the event of the Form1 in the
Form2 when the Form2 is closed.
Hope this helps.
If you need our further assistance, please send me your sample project that
could just reproduce the problem. To get the actual email address, remove
'online' from my displayed email address.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.