Posted on 2008-11-01
Last Modified: 2013-11-17

I use some custom controls on my CF2 Form I add dynamic and need to remove dynamic from my Form.
The matter is that I get an ObjectDisposedException when I dispose it at some of it's events.

So in the constructor I apply some events to the parent form eg.:
this.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);

When I dispose it I remove these events:
protected override void Dispose(bool disposing)
   this.Parent.MouseUp -= Parent_MouseUp;

However this don't solve the problem.
What works for me is to catch the ObjectDisposedException but I think this is not the cleanest way to make it working ...

Maybe somebody know a better solution?


Question by:andre72
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
  • 3
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22857473
you don't need that. Dispose() function is intended to free non-managed resources like GDI or database resources.
The events will be removed without your intervention when the object is no longer used.

Author Comment

ID: 22857484
Thanks for the information so you've also any idea about the Exception?
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22857647
the exception is produced because the parent window was disposed at the moment the line of code is excecuted. But as I said, what you are doing is not needed.
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.


Author Comment

ID: 22857681
Sorry for missunderstanding - it doesn't matter if I include it or not. Because of your hint I removed the lines as the dispose do it itself ...
But the problem is still the same ...
LVL 55

Accepted Solution

Jaime Olivares earned 250 total points
ID: 22859907
remove all the dispose method.

Author Comment

ID: 22862266
The control get it's events from the parent control:
                this.Parent.MouseDown += new MouseEventHandler(Parent_MouseDown);
                this.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);
                this.Parent.MouseMove += new MouseEventHandler(Parent_MouseMove);

Isn't there a way to clear any event of it and / or clear the message queue?

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but breaks when I add one section 4 30
VS2010 Build fails to install 14 82
C# XML Get Values 4 35
Visual C# Windows Project - Clone Form 4 26
In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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