Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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