Click here to Skip to main content
12,627,002 members (32,327 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi,

my program is made in Windows Form C# and what i do is i have a main form, that calls onto other forms and opens then and hiddes itself:

private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 fm2 = new Form2();
            this.Hide();
            fm2.Show();
        }

and when i close form2 it closes and opens Form 1

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form1 fm1 = new Form1();
            fm1.Show();            
        }

but my program has many forms and i do that many times, and it work fine and everything but when it gets time to close the hole program it takes a while and it closes many times apperanlty form1 it blinks and if i use it a lot it tames more time, its like if everytime a open and close it remain open and at the end it needs to close all. i close whit

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

any help?? thanks
Posted 21-Dec-12 12:35pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You are creating a new Form1 every time you close Form2. Just pass a reference to Form2 instead.

If you need more explaining let me know and I will give you an example of how to accomplish this.

EDIT:
OK here is an example of a way to accomplish this.

Add this variable to your Form2 class "
private Form1 form1;

and in your Form2 constructor do this:

public Form2(Form1 form1)
{
    this.form1 = form1;
}

then in your Form2_FormClosed event handler do this:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    form1.Show();
}

EDIT 2:
Some sample code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 f2 = new Form2(this);
            f2.Show();
        }
    }

public partial class Form2 : Form
    {
        Form1 form1;
 
        public Form2(Form1 form1)
        {
            InitializeComponent();
 
            this.form1 = form1;
        }
 
        private void Form2_Load(object sender, EventArgs e)
        {
 
        }
 
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            form1.Show();
        }
    }



Hope this will help you understand.
  Permalink  
v3
Comments
azteca_04 21-Dec-12 17:44pm
   
Yes can you give me an example please
SS4L84 21-Dec-12 17:53pm
   
Recheck my solution.
SS4L84 21-Dec-12 18:14pm
   
Check my solution again I have added some more sample code for you.
azteca_04 21-Dec-12 18:17pm
   
Thank you i would check it as soon as i get home, thanks for the quick reply
SS4L84 21-Dec-12 18:32pm
   
You're welcome. Hope it helps.
azteca_04 25-Dec-12 15:43pm
   
Hi, the solotion you have me, it does work for part of it, since from form1 i open form2 but from form2 i open form3 and i have it return to form1 after its down but with the solution it does not work it says something like cant have 1 for contruct or something like that, any ideas??

thanks again
jibesh 21-Dec-12 19:59pm
   
since you mentioned you open and close these forms many times I would recommend to keep Form2 as a member of Form1 if possible to avoid unnecessary memory usage to the above solution.
SS4L84 21-Dec-12 20:34pm
   
Yes that is good advice. I also recommend doing this.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2016
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