Solved

Creating multiple forms

Posted on 2011-09-16
13
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 86

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
 
LVL 86

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 86

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 86

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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