4

I have two forms; one called 'win' and the other called 'loss'. There is a button on 'win' form which displays the 'loss' form. When this button is clicked both forms are visible. When I close the 'loss' form and then click the button on the 'win' form again I get the following exception:

An unhandled exception has occured: Unable to access a disposed object ..object :form

Please could someone point me in the right direction so I can resolve this?

5
  • 1
    Are yo u sure u are creating a new instance of the Form every time the buttn is clicked... Commented Jan 24, 2011 at 13:14
  • definately need to know more of the code Commented Jan 24, 2011 at 13:22
  • What are you trying to achieve? 'win' form should retrieve some data from 'loss' form? Commented Jan 24, 2011 at 13:39
  • I wana 2 call loss form from win form as many times as we click on dat btn.. Commented Jan 24, 2011 at 13:41
  • Call == create and show new loss form, OR you want to call some functionality on one loss form? BTW win form should be accessible (e.g. receive user input)? Commented Jan 24, 2011 at 13:42

3 Answers 3

4

It is because your 'loss' form is already closed and has been disposed, so it cannot be used anymore. You need to create a new instance of the form, like so (don't know how exactly your code looks):

this.loss = new LossForm(); 
this.loss.Show();
Sign up to request clarification or add additional context in comments.

5 Comments

And what if I click 5 times that button? It will create 'loss' form 5 times.
public void btn_Log(Object sender,System.EventArgs e) { Form loss = new Form(); loss.Text = "Advance View " ; loss.Controls.Add(panC); loss.Size=new Size(320,330); fl.Show(); } bLog.Click+=new EventHandler(btn_Log);//btn calls loss form...all these fun are in class win :Form ..plz rly nw
@Mukul if you add a control to your form on creation, make sure you have also created that control newly. The control 'panC' will get disposed along with the 'loss' form, so you'll need a new panC control for each loss form.
yah,i agreed..bt panC control is dere in class win :Form ...SO hw many times i hav 2 create it ? becz we cant predict hw many times the btn is clicked
you have to create it every time you create a loss form. because when the loss form is closed, panC will get disposed, and will not be useable anymore.
1

You can verify IsDisposed property of form, before referencing it.

E.g. button click handler on 'win' form:

if (loss.IsDisposed)
  return;

// do stuff with loss form

Update: I think it's better not to share control between forms.

  1. You can run 'loss' form as Dialog. And read all needed properties after dialog closed.
  2. You can subscribe to 'loss' form events and process them in 'win' form.

3 Comments

Of course, that won't open the loss form a second time. It just prevents the exception from being thrown.
I wana 2 call loss form from win form as many times as we click on dat btn.
Why don't just put panC control to 'loss' form in design time? It will be created and added to loss.Controls automatically. Sharing control between forms is not very good idea. You can pass data via events of properties of form.
0

It's not a very good model your going for but you could hook into the formClosing event, cancel it and then hide the form instead. That means the form wont be automatically disposed and you could call show again.

Put some time aside to research MVC architecture - it looks complicated at first, but it really does help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.