Desposing Form problem
-
I want to dispose my previous form on the after select event of tree view control but when i try to do so i get the following exception message System.ObjectDisposedException: Cannot access a disposed object named "TreeView". Object name: "TreeView". I m building Windows application In C# 2003.Thanx for Ur help
-
I want to dispose my previous form on the after select event of tree view control but when i try to do so i get the following exception message System.ObjectDisposedException: Cannot access a disposed object named "TreeView". Object name: "TreeView". I m building Windows application In C# 2003.Thanx for Ur help
Sounds like you're disposing too soon. Can we see some code ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Sounds like you're disposing too soon. Can we see some code ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
private void TreeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { switch(name) { case "1": { Form3 f=new Form3(); f.Show(); this.close(); break; } } } My code is like this. I am opening new form and disposing the current form.
-
Hello, If 'this' is the main form, you are not able to Close (Dispose) without ending the Application. All the best, Martin
-
"this" is not my main form. i know i cant close my main form. This is just another form which i want to close.
-
private void TreeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { switch(name) { case "1": { Form3 f=new Form3(); f.Show(); this.close(); break; } } } My code is like this. I am opening new form and disposing the current form.
OK, this can't work. You're opening an instance of Form3 ( please tell me it's not called that ). But, that instance exists as a child of the current form. So, when you close the current form, the Form3 instance, as a child, will also be disposed of. The Show() method should only be used to show a form that is a member variable, otherwise you lose all references to it. Your best bet here is to make Form3 and this form user controls and show them both on teh one form, just hide the one you don't want.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
OK, this can't work. You're opening an instance of Form3 ( please tell me it's not called that ). But, that instance exists as a child of the current form. So, when you close the current form, the Form3 instance, as a child, will also be disposed of. The Show() method should only be used to show a form that is a member variable, otherwise you lose all references to it. Your best bet here is to make Form3 and this form user controls and show them both on teh one form, just hide the one you don't want.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Hello Christian! I also had this in mind, but made a little test application, which works well for me.
Christian Graus wrote:
So, when you close the current form, the Form3 instance, as a child, will also be disposed
I don't think it's a child Form per default! I assume there is some other code executed on Form2 after the Close call. All the best, Martin