Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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

C#
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

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


any help?? thanks
Posted

1 solution

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 "
C#
private Form1 form1;


and in your Form2 constructor do this:

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


then in your Form2_FormClosed event handler do this:

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


EDIT 2:
Some sample code:

C#
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();
        }
    }


C#
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.
 
Share this answer
 
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.

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