Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Desposing Form problem

Desposing Form problem

Scheduled Pinned Locked Moved C#
helpcsharpdata-structures
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EEmaan
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • E EEmaan

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      E 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        E Offline
        E Offline
        EEmaan
        wrote on last edited by
        #3

        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.

        M C 2 Replies Last reply
        0
        • E EEmaan

          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.

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, If 'this' is the main form, you are not able to Close (Dispose) without ending the Application. All the best, Martin

          E 1 Reply Last reply
          0
          • M Martin 0

            Hello, If 'this' is the main form, you are not able to Close (Dispose) without ending the Application. All the best, Martin

            E Offline
            E Offline
            EEmaan
            wrote on last edited by
            #5

            "this" is not my main form. i know i cant close my main form. This is just another form which i want to close.

            M 1 Reply Last reply
            0
            • E EEmaan

              "this" is not my main form. i know i cant close my main form. This is just another form which i want to close.

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, Is the some other code executed after the switch case statement? Try to debug! All the best, Martin

              1 Reply Last reply
              0
              • E EEmaan

                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.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                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 )

                M 1 Reply Last reply
                0
                • C Christian Graus

                  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 )

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups