从第二张表格中退出C#申请

从第二张表格中退出C#申请,c#,C#,当用户按下X按钮时,如何从第二个表单完全退出C#应用程序 这是我目前的代码: Form1.cs private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 1) { String cellValue; cellValue = dataGridView1[e

当用户按下X按钮时,如何从第二个表单完全退出C#应用程序

这是我目前的代码:

Form1.cs

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 1)
        {
            String cellValue;
            cellValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            var form2 = new Form2(cellValue);
            this.Hide();
            form2.Show();
        }
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);

        if (e.CloseReason == CloseReason.WindowsShutDown) return;

        switch (MessageBox.Show(this, "Are you sure you want to exit?", "", MessageBoxButtons.YesNo))
        {
            case DialogResult.No:
                e.Cancel = true;
                break;
            default:
                break;
        }
    }
protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e);

    if (e.CloseReason == CloseReason.WindowsShutDown) return;

    switch (MessageBox.Show(this, "Are you sure you want to exit?", "", MessageBoxButtons.YesNo))
    {
        case DialogResult.No:
            e.Cancel = true;
            break;
        default:
            break;
    }
}
Form2.cs

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 1)
        {
            String cellValue;
            cellValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            var form2 = new Form2(cellValue);
            this.Hide();
            form2.Show();
        }
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);

        if (e.CloseReason == CloseReason.WindowsShutDown) return;

        switch (MessageBox.Show(this, "Are you sure you want to exit?", "", MessageBoxButtons.YesNo))
        {
            case DialogResult.No:
                e.Cancel = true;
                break;
            default:
                break;
        }
    }
protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e);

    if (e.CloseReason == CloseReason.WindowsShutDown) return;

    switch (MessageBox.Show(this, "Are you sure you want to exit?", "", MessageBoxButtons.YesNo))
    {
        case DialogResult.No:
            e.Cancel = true;
            break;
        default:
            break;
    }
}

根据评论,听起来这是一个可行的解决方案:

private bool userRequestedExit = false;
protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e);

    if (this.userRequestedExit) {
        return;
    }

    if (e.CloseReason == CloseReason.WindowsShutDown) return;

    switch (MessageBox.Show(this, "Are you sure you want to exit?", "", MessageBoxButtons.YesNo))
    {
        case DialogResult.No:
            e.Cancel = true;
            break;
        default:
            this.userRequestedExit = true;
            Application.Exit();
            break;
    }
}

根据评论,听起来这是一个可行的解决方案:

private bool userRequestedExit = false;
protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e);

    if (this.userRequestedExit) {
        return;
    }

    if (e.CloseReason == CloseReason.WindowsShutDown) return;

    switch (MessageBox.Show(this, "Are you sure you want to exit?", "", MessageBoxButtons.YesNo))
    {
        case DialogResult.No:
            e.Cancel = true;
            break;
        default:
            this.userRequestedExit = true;
            Application.Exit();
            break;
    }
}

由于某种原因,该对话框出现三次,用户必须单击“是”每一次,否则程序就会崩溃,现在请告诉我是否出了问题yetSame事情-真的很奇怪:-SMight值得补充的是,我在Form1.cs上有此代码-尝试使用环境退出作为应用程序。退出生成循环消息由于某种原因,对话框会出现三次,用户必须单击“是”否则每次程序都会崩溃,请立即重试,如果出现问题请告诉我YetName事情-真的很奇怪:-SMight值得补充的是我在Form1.cs上有此代码-尝试将环境退出作为应用程序。退出生成循环消息