Solved

Creating multiple forms

Posted on 2011-09-16
13
240 Views
Last Modified: 2013-12-17
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
Comment
Question by:peterkiers
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 36550128
Close Button should be this.Dispose()
0
 
LVL 40
ID: 36550248
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
 
LVL 1

Author Comment

by:peterkiers
ID: 36550287
I have use this.Dispose for the close-button.
But I get the same error.

Peter
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36550309
Surely you have more code than that...

...probably somewhere down below you are attempting to access "frmNote"?
0
 
LVL 15

Expert Comment

by:x77
ID: 36550333
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
 
LVL 1

Author Comment

by:peterkiers
ID: 36550369
Idle_Mind: Yes, that's all i have.

P
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36550382
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
 
LVL 1

Author Comment

by:peterkiers
ID: 36550399
I get the error at line:

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

Peter
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36550421
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
 
LVL 1

Author Comment

by:peterkiers
ID: 36550519
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36550531
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
 
LVL 1

Author Comment

by:peterkiers
ID: 36550564
Yes, thank you.
500 points are going to Idle_Mind

Greetings,

Peter Kiers
0
 
LVL 40
ID: 36550578
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now