Solved

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

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

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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