Click here to Skip to main content
15,909,440 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
when timer finished increment i want to close that form that is Form2



C#
namespace WindowsFormsAppDAL
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Form3 f3 = new Form3(); 

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (progressBar2.Value < 100)
            {
                progressBar2.Value += 2;
            }
            else
            {
                timer1.Stop(); 
                f3.ShowDialog();
            }
            
           
        }
    }
}
Posted

You can't, not with that code. ShowDialog is a modal call, which means that it waits until the form being shown has closed before it continues executing - which means you can't close the form2 until form3 is finished. If you used Show instead, it would return immediately and you could use Close on the form, but that would have unpredictable effects since the instance of form3 is a part of form2, and if one is destroyed, then the other could go too.
There are two approaches which could do what you want:
1) Hide your form2, and close it after:
C#
private void timer1_Tick(object sender, EventArgs e)
{

    if (progressBar2.Value < 100)
    {
        progressBar2.Value += 2;
    }
    else
    {
        timer1.Stop();
        Hide();
        f3.ShowDialog();
        Close();
    }
}

2) Signal back to the parent form that you are finished, and would like form3 to be shown, by creating an event which the parent handles, and signalling it before closing yourself. The parent then decides to show the form and everything works happily.

I would go with the second option, and it isolates things better.
 
Share this answer
 
Comments
Member 9411249 20-Nov-12 2:05am    
Thank you so much :)
OriginalGriff 20-Nov-12 2:31am    
You're welcome!
C#
private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value += 1;

            if (progressBar1.Value == progressBar1.Maximum)
            {
                timer1.Enabled = false;
                this.Close();            
            }

        }
 
Share this answer
 

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