?
Solved

Problem with a handler firing after it's parent form has closed. How do I stop this?

Posted on 2008-11-02
4
Medium Priority
?
151 Views
Last Modified: 2008-11-02
Hi experts

Here's the summary of my problem:

My application opens a new form (of Class 'note') for each item in a database it loads at startup. In Public Class note the handlers for the control events are added at runtime (AddHandler). There is an event in Class note to remove the form, and its corresponding row in the dataset. Then the relevant form closes.

All this works fine but one of the control events defined in the Public Class note triggers as soons as the form closes. It is the Textbox.LostFocus event. The functions of this event throw an exception because, of course, the appropriate line in the database doesn't exist.

I can of course manually remove the handlers in the FormClosing event, but I'm curious as to why the handlers are still there. Is this something to do with the fact that they are defined at runtime?

Thanks in advance.

0
Comment
Question by:jerute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22861719
<!--[if gte mso 9]>   Normal  0          false  false  false    EN-US  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     <![endif]--><!--[if gte mso 9]>                                                                                                                                                                                                                                                                                    <![endif]--><!--[if gte mso 10]><![endif]-->Hi jerute;

To your statement, "I can of course manually remove the handlers in theFormClosing event, but I'm curious as to why the handlers are still there. Isthis something to do with the fact that they are defined at runtime?" I donot know for a fact but I would say it is and this is why. When you define acontrol on a form at design time the control is defined using the WithEventskeyword therefore Microsoft may be disabling the events when a request is madeto close the form. The most likely cause of the problem is that the LostFocuswas raised and put into the queue to be posted to the event handler but not yetprocessed and before it was processed the form closed. Because the form is notGarbage Collected immediately and is still available the event gets raised. Theanswer to the question is to use the RemoveHandler before starting the processof closing the form.

Fernando
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 22861749
Hi jerute;

Sorry about the last post this new Rich text box editor still needs some work on it, I copied and paste to here, hope they get it fixed soon.

To your statement, "I can of course manually remove the handlers in the FormClosing event, but I'm curious as to why the handlers are still there. Is this something to do with the fact that they are defined at runtime?" I do not know for a fact but I would say it is and this is why. When you define a control on a form at design time the control is defined using the WithEvents keyword therefore Microsoft may be disabling the events when a request is made to close the form. The most likely cause of the problem is that the LostFocus was raised and put into the queue to be posted to the event handler but not yet processed and before it was processed the form closed. Because the form is not Garbage Collected immediately and is still available the event gets raised. The answer to the question is to use the RemoveHandler before starting the process of closing the form.

Fernando
0
 

Author Comment

by:jerute
ID: 22862281
Thanks for your explanation. That makes a lot of sense actually.

Sure enough, using the RemoveHandler method, it all works fine, though I had to deal with the 'AddressOf expression has no effect...requires a relaxed conversion' etc. etc so defined a couple of New EventHandler() delegates to add and remove the handlers.

Cheers Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22862356
Not a problem, glad I was able to help.  ;=)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question