• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Creating multiple forms

Hi,

I have a main form called mainForm and a second form called StickyNotes.
On the mainForm i have just one toolbar-button to call for the form StickyNotes.
And on the form StickyNotes I have just a close-button. With this line in the On-
Click methode:

Close();

And in the methode of the toolbar-button I have this line:

StickyNotes frmNote = new StickyNotes();
frmNote.Show();

But after pressing multilpe times on the toolbar-button I get an error:
"Cannot access a disposed object. Object: "StickyNotes".

What do i do wrong? How can i open multiple forms by pressing on the
toolbarbutton.

Peter
0
peterkiers
Asked:
peterkiers
  • 5
  • 4
  • 2
  • +2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Close Button should be this.Dispose()
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Calling Dispose is surely not the solution, since the problem is that the object is already disposed.

Calling Dispose on a form is also useless, since Close calls Dispose. You can check this by disabling the DebuggerStepThrouhg attribute in the form.designer file and going step by step.

On which line do you get the error?
0
 
peterkiersAuthor Commented:
I have use this.Dispose for the close-button.
But I get the same error.

Peter
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Surely you have more code than that...

...probably somewhere down below you are attempting to access "frmNote"?
0
 
x77Commented:
You can use AddOwnedForm method to set the new form owned from you mainform.

Then when you want close any owned form

Enumerate OwnedForms and

Use RemoveOwnedForm and close the form.

When you close the mainform, all owned form are closed.
0
 
peterkiersAuthor Commented:
Idle_Mind: Yes, that's all i have.

P
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The error is NOT in these lines:

    StickyNotes frmNote = new StickyNotes();
    frmNote.Show();

It is being cause by code somewhere else.

James asked earlier, "On which line do you get the error?"
0
 
peterkiersAuthor Commented:
I get the error at line:

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmNote.Show(); <-----------
        }

Peter
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's not the code you posted earlier!

This new code is using a variable declared at CLASS level (not in the method).

You'd need to do something like this to fix that:


private StickyNotes frmNote;

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (frmNote == null || frmNote.IsDisposed)
            {
                frmNote = new StickyNotes();
            }
            frmNote.Show();
        }

Open in new window

0
 
peterkiersAuthor Commented:
Now there is no error.

I open a new form and i can close it.

But is it possilbe to display mulitple forms of the same instance (StickyNotes).

Peter
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Of course it is...using the code you originally posted!
*If you have other code that was reference "frmNote" it is going to cause problems!

private void toolStripButton1_Click(object sender, EventArgs e)
{
    StickyNotes frmNote = new StickyNotes();
    frmNote.Show();
}

Open in new window

0
 
peterkiersAuthor Commented:
Yes, thank you.
500 points are going to Idle_Mind

Greetings,

Peter Kiers
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
An instance a a form. You cannot have many forms on the same instance. Idle_Mind is right, your first code should work. That is why I asked which line caused the error, because in such a simple piece of code, everything looked right. It was.

When you ask a question, send the exact code. Too many posters send code that "looks" or do the same thing as the original. Doing so, they often remove the source of the problem.

I take care of testing almost any code that I post as an answer, because very small things often have a big impact.

It out be nice if the programmers who post questions would also post code that has been tested to be defective.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now