Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
4.60/5 (2 votes)
See more:
I have a code:
private Form2 f2=null;
  private void f2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (f2 == null)
            {
                f2 = new Form2();
                f2.Show();
            }
            else
            {
                f2.Activate();
            }
        }
        }

The thing is, when i click on f2ToolStripMenuItem, it shows me the Form2 - this is ok, if i don`t close the form2 and click again f2ToolStripMenuItem it brings form2 on top - this is also ok. But if i close the form2, and click f2ToolStripMenuItem, nothing happens it does not show form2 again.

And i had a sample of code, where the error was something about can`t run disposed... (something like that)

How to fix this? I don`t want to use singletone.

For example form2 is "About" like in any application. When shown, just brings it to front for every click, but not making form for every click.
Posted

the reason is that first time when it shows the form2 it changes f2 value now which is not null and after closing it and executing the command again f2 is not null so it will not run the command.

To fix this write this
private Form2 f2=null;
  private void f2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (f2 == null)
            {
                f2 = new Form2();
                f2.ShowDialog();
                f2=null;
            }
            else
            {
                f2.Activate();
            }
        }
        }
 
Share this answer
 
Comments
sino99 8-Mar-14 11:45am    
Try simple way like this!
Gook luck.
C#
private Form f = null;
private void button1_Click(object sender, EventArgs e)
{
    //case 1
    if (f == null || !f.Visible)
    {
        f = new Form2();
    }
    f.Show();
}
 
Share this answer
 
If you want to show one form at a time, the design with multiple forms is bad in principle.

You still can use multiple forms and show one form at a time if you never close forms (except the application exit), only hide them. Then you just control the visibility of the form. To prevent closing of the form (you could not open it again, will see the exception you already mentioned, because the form is disposed) by hiding it instead of closing. This is done by handling the event FormClosing and setting event argument's Cancel to true.

It will work, but still, I don't think this is a good solution. The good solution would be this: use just one form in the whole application. You still have something which are forms right now. Makes them panels, each docked in the same form. Make all of those panels visible one at a time. As simple as that.

And, finally, you can use the simplest approach: the form has one control on top, TabControl, and its tab pages will house what now your forms do.

—SA
 
Share this answer
 
Comments
Jibesh 16-Dec-12 22:26pm    
Great Sergey. thats what I did long time back when I faced similar situation.

5!!
Sergey Alexandrovich Kryukov 16-Dec-12 23:19pm    
Thank you.
—SA
Activate does not show the form. You should call show in both cases.
 
Share this answer
 
Under Button which may be on form1 or any other form just enter the following lines.

try
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm.Name.ToString().Equals("Form2"))
{
Application.OpenForms["Form2"].Close();
break;
}

}

Form2 f2= new Form2();
f2.Show();

}
catch(Exception)
{}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900