Solved

Creating multiple forms

Posted on 2011-09-16
13
239 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

13 Experts available now in Live!

Get 1:1 Help Now