简体   繁体   中英

C# Close windows form

I am opening up another form when a button is clicked, but can't decide how to close the current form when doing this. This is the code:

private void nextSportButton_Click(object sender, EventArgs e)
{
    for (int i = 0; i < Form1.sportsAdded; i++)
    {
        if (Form1.sportOpened == i)
        {
            Form1.IDNumber = Form1.sportIDArray[i];
            OutputForm OutputForm = new OutputForm();
            OutputForm.ShowDialog();
        }

        this.Close();
    }
}

Calling OutputForm.ShowDialog() waits until OutputForm is closed and only then returns to execute further code. You want to use OutputForm.Show() instead.

Note: If "this" is the main form (the first form shown) of your application, closing "this" will terminate the whole application.

first hide current form don't close after open your new form,

this.Hide();

Form1.IDNumber = Form1.sportIDArray[i];
OutputForm OutputForm = new OutputForm();
OutputForm.ShowDialog();

Use this.Hide(); instead of this.Close(); and using this.Close(); inside a for loop should throw an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM