Silver Hawk
Silver Hawk

Reputation: 57

Error with showing a new form in C#

I need your help please
I have created a form and insert a timer & progress bar into it
when the value of progress bar reach to 100% I want to close this form and open the main form of my program
I write this code but when I Run the program it show this error :
( Form that is already displayed modally cannot be displayed as a modal dialog box. Close the form before calling showDialog.)
How I can resolve this problem

    Form1 MainForm = new Form1();
    public Welcome_window()
    {
        InitializeComponent();
        timer1.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(10);
        if (progressBar1.Value == 100)
        {
            this.Visible = false;
            MainForm.Visible = false;                                       
            MainForm.ShowDialog();
            this.Close();                          
          }
    }
}

Upvotes: 0

Views: 240

Answers (1)

user7861944
user7861944

Reputation:

I think the problem is that you don't stop the timer, so the tick event will be fired even if the progress already reached 100%.

Form1 MainForm = new Form1();
public Welcome_window()
{
    InitializeComponent();
    timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
    progressBar1.Increment(10);
    if (progressBar1.Value == 100)
    {
        timer1.Stop(); 

        this.Visible = false;                                      
        MainForm.ShowDialog();
        this.Close();                
    }
}

Upvotes: 2

Related Questions