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. error:cannot access disposed object

error:cannot access disposed object

Scheduled Pinned Locked Moved C#
helpquestion
3 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.
  • S Offline
    S Offline
    shefa isied
    wrote on last edited by
    #1

    Hi.....please some one help me in my problem i have tow forms the 1st has a button that opens the 2nd form, on form closing event for 2nd form i called Hide() function ,so when i click the button again(to Show() 2nd form) it gives me the following error:cannot access a disposed object) i do not know what to do?can it be to stop 2nd form from calling Dispose () when i Hide() it? or it would be some thing else? by the way, i have to do what i did in this way, so any help is appreciated

    D M 2 Replies Last reply
    0
    • S shefa isied

      Hi.....please some one help me in my problem i have tow forms the 1st has a button that opens the 2nd form, on form closing event for 2nd form i called Hide() function ,so when i click the button again(to Show() 2nd form) it gives me the following error:cannot access a disposed object) i do not know what to do?can it be to stop 2nd form from calling Dispose () when i Hide() it? or it would be some thing else? by the way, i have to do what i did in this way, so any help is appreciated

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      The form is automatically disposed when it's closed. You can set e.Cancel to true in the FormClosing handler, but make sure you provide a way to eventually really close the form (by some boolean check maybe) otherwise it will just sit there in memory but invisible and cause you all sorts of problems.

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      1 Reply Last reply
      0
      • S shefa isied

        Hi.....please some one help me in my problem i have tow forms the 1st has a button that opens the 2nd form, on form closing event for 2nd form i called Hide() function ,so when i click the button again(to Show() 2nd form) it gives me the following error:cannot access a disposed object) i do not know what to do?can it be to stop 2nd form from calling Dispose () when i Hide() it? or it would be some thing else? by the way, i have to do what i did in this way, so any help is appreciated

        M Offline
        M Offline
        Moreno Airoldi
        wrote on last edited by
        #3

        By what you say I suppose you have a reference in Form1 to Form2, something like:

        class Form1 : Form
        {
        ...
        Form2 RefToForm2 = new Form2();
        ...
        private void ButtonToShowForm2_Clicked(...)
        {
        RefToForm2.Show();
        }
        ...
        }

        Now, as Dave said in his answer, you can use e.Cancel = true to avoid automatic disposal of Form2, but I think you should first consider if you need your instance of Form2 to stay in memory or you can simply create a new instance every time you need to show Form2. The latter is usually the best practice. So, if you store some value in Form2 and you don't want to lose it, you should store it somewhere else and recover it when you load Form2. Then, you can simply do:

        class Form1 : Form
        {
        ...
        private void ButtonToShowForm2_Clicked(...)
        {
        Form2 RefToForm2 = Application.OpenForms["Form2"];
        if (RefToForm2 == null) RefToForm2 = new Form2();
        RefToForm2.Show();
        RefToForm2.Activate();
        }
        ...
        }

        Or something similar. Hope this can help. :)

        2+2=5 for very large amounts of 2 (always loved that one hehe!)

        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