Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# form
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 16-Dec-12 13:36pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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();
            }
        }
        }
  Permalink  
Comments
sino99 at 8-Mar-14 11:45am
   
Try simple way like this!
Gook luck.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

 
        private Form f = null; 
        private void button1_Click(object sender, EventArgs e)
        {
            //case 1
            if (f == null || !f.Visible)
            {
                f = new Form2();
            }
            f.Show();
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
jibesh at 16-Dec-12 22:26pm
   
Great Sergey. thats what I did long time back when I faced similar situation.
 
5!!
Sergey Alexandrovich Kryukov at 16-Dec-12 23:19pm
   
Thank you.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Activate does not show the form. You should call show in both cases.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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)
{}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 8 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100