Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone....
This is my problem

 public partial class Expedientes : UserControl
    {
        FormX AddNewForm = new FormX();

   private void button1_Click(object sender, EventArgs e)
   {
    AddNewForm.button1.Click+= new EventHandler(XEvent);
    AddNewForm.Show(); 
   }

  private void button2_Click(object sender, EventArgs e)
   {
     AddNewForm.Close();
   }

  private void XEvent(object sender, EventArgs e)
   {
    this.textBox1.Text=AddNewForm.textBox1.Text;
   }

}


When I close the form and try to reopen it, this error occurs:
Can not access a disposed object.

How I solve can it?

Thanks people
Posted

Hi instead of using form close use Form.Hide(); . Other wise each time create a new instance in the button click event. Once the form close called. it get dispose automatically
 
Share this answer
 
Comments
TANicox 14-Feb-11 22:51pm    
I can't to do that because I have others instructions in the button
And if insert XForm.Hide();
And later I open the Form again, the data copy twice
Abhinav S 14-Feb-11 22:55pm    
Good answer Albin. My 5.
Albin Abel 14-Feb-11 23:20pm    
What you mean by data copy twice. When you hide the form it is just not visible. But the instance is still there. when you show it become visible. Thats all. Please explain more

may be move this to event where you create the instance.. (may the user control's constructor)
AddNewForm.button1.Click+= new EventHandler(XEvent);

in the Button event you are adding it again and again to the same form
TANicox 14-Feb-11 23:26pm    
I have 2 forms.
One have the database And the other works as data inserter. If I insert an .Hide, later, when I add new data, this is added twice
Albin Abel 14-Feb-11 23:29pm    
if the data is same no need to add again and again. when hide and show, you are using only one instance. So make check, if the data is not added earlier then only add it or remove those data before adding again. See my above comment for the code fragment you shown here. If you have further problems post more codes.
I think you need to look at why this data is copied when you hide and then show the form again.
Try moving that logic out of its current position into some other place.
 
Share this answer
 
Comments
Albin Abel 14-Feb-11 23:23pm    
my 5 too for giving good suggestions
TANicox 14-Feb-11 23:23pm    
Look. I have 2 forms
One have the database
And the other works as data inserter

understand?
Try this,

C#
public partial class Expedientes : UserControl
    {
    FormX AddNewForm = null;
   private void button1_Click(object sender, EventArgs e)
   {
    AddNewForm = new FormX();
    AddNewForm.button1.Click+= new EventHandler(XEvent);
    AddNewForm.Show();
   }
  private void button2_Click(object sender, EventArgs e)
   {
     if (AddNewForm!=null)
         AddNewForm.Close();
   }
  private void XEvent(object sender, EventArgs e)
   {
    this.textBox1.Text=AddNewForm.textBox1.Text;
   }
}


Mark it as answer if it is helpful.
 
Share this answer
 
Comments
TANicox 15-Feb-11 0:08am    
Thanks!
I really really appreciate your answer!
That work
Thanks a lot
Really
As per the Venkatesh Mookkan answer, it's fine but check it on a condition.
open your form and click on button1 it will open another form, as many as time you will click(without closing the form) it will open a new form, which is accually not a good thing. so just check a condition in button1_click()


if(AddNewForm==null)
AddNewForm = new FormX();
AddNewForm.button1.Click+= new EventHandler(XEvent);
AddNewForm.Show();
 
Share this answer
 
Comments
Venkatesh Mookkan 15-Feb-11 3:04am    
Praveen, Close() would Dispose the object. Dispose object will not be null. So, his problem will not be solved by your solution.
Praveen Kumar Upadhyay 15-Feb-11 3:51am    
yes Venkatesh you are right, but please go through my answer, i have mentioned it that if you have not closed your form and trying to open another form by clicking on button1 then it wud cause a problem
Venkatesh Mookkan 15-Feb-11 21:22pm    
OK. I missed it. But, I am sure, I am not the Uni-voter.
C#
if (AddNewForm != null)
{
   AddNewForm = new FormX();
   AddNewForm.button1.Click+= new EventHandler(XEvent);
   AddNewForm.Show();
}
else
{
   AddNewForm.button1.Click+= new EventHandler(XEvent);
   AddNewForm.Show();
}


building button1.Click like this will help. if u prevent multiple instances u need this. Because a fresh form is null but a closed one is not. so if it is not null it means a closed one and needs to be recreated. otherwise null one means a fresh one and showing will work fine.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900