Solved

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

Posted on 2008-11-02
4
145 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

22 Experts available now in Live!

Get 1:1 Help Now