Solved

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

Posted on 2008-11-02
4
142 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
  • 3
4 Comments
 
LVL 62

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 62

Accepted Solution

by:
Fernando Soto earned 250 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 62

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now