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