C# “关闭”按钮未正确关闭窗体

C# “关闭”按钮未正确关闭窗体,c#,winforms,datagridview,C#,Winforms,Datagridview,我的表单包含一个名为“关闭”的按钮。我只是把下面的代码在关闭按钮点击。。。然后,第二个代码集用于表单关闭。但如果我在单击关闭按钮时执行此操作,则会出现一个消息框。当我单击“是”按钮时,表单不会关闭,但如果我第二次单击“是”按钮,表单将关闭。原因是什么?你能帮帮我吗 private void btnClose_Click(object sender, EventArgs e) { if (MessageBox.Show("Are You Sure You Want To Close Thi

我的表单包含一个名为“关闭”的按钮。我只是把下面的代码在关闭按钮点击。。。然后,第二个代码集用于表单关闭。但如果我在单击关闭按钮时执行此操作,则会出现一个消息框。当我单击“是”按钮时,表单不会关闭,但如果我第二次单击“是”按钮,表单将关闭。原因是什么?你能帮帮我吗

private void btnClose_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Are You Sure You Want To Close This Form?", 
                        "Close Application", 
                         MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        // MessageBox.Show("The application has been closed successfully.", 
        //                 "Application Closed!", 
        //                  MessageBoxButtons.OK);
        System.Windows.Forms.Application.Exit();
    }
    else
    {
        this.Activate();
    }
}

-------------------------------------   

private void frminventory_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("Are You Sure You Want To Close This Form?", 
                        "Close Application", 
                         MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        System.Windows.Forms.Application.Exit();
    }
    else
    {
        this.Activate();
    }
}
不关闭/退出应用程序,但填写以下表格:

编辑:通常我们直接向用户提问,如果我只是关闭此表单,不清楚会出现什么错误,例如:


所以,因为我不能发表评论,我会这样做

    //Create this variable
    private bool _saved = true;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        DoSomething();
        _saved = true;
    }

    //Raised when the text is changed. I just put this for demonstration purpose.
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        _saved = false;
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (_saved == false)
        {
            var result = MessageBox.Show("Are you sure you want to close?\nYou may have unsaved information", "Information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            if (result == DialogResult.Yes)
            {
                _saved = true;
                Application.Exit();
            }
            else
                e.Cancel = true;
        }
    }

希望这可以解决您的问题

我的表单包含一个数据网格,如果我向该数据网格添加详细信息,并单击“关闭”按钮而不保存,则详细信息将丢失,对吗?表单关闭单击右键时也会发生同样的情况?这就是为什么我要为“关闭按钮单击”和“表单关闭事件”添加确认消息。@sherin:是的,当表单即将关闭时,请询问用户是否要保存更改,并在必要时取消关闭。但是,我们通常会直接问:是否要保存更改?是/否/取消,其中是-保存更改并关闭表单,否-放弃更改并关闭表单,取消-不关闭表单。先生,谢谢你的代码。但问题是,当对数据网格进行任何更改并单击“保存”按钮时,会显示一条类似“已保存”的消息,然后还会显示“表单关闭”消息框。问题是什么issue@sherin:是的,您必须支持isEdited字段、属性等。如果用户更改了网格,请将其设置为true,在“保存”中将其设置为false…谢谢。是否可以提供一个代码,将一个数据网格中已编辑的行显示到另一个数据网格?假设我对整个行将显示在其他数据网格上的任何行进行任何更改时,数据网格1包含一些详细信息。该代码必须在单元格中更改值?请帮帮我……现在我帮不了你,对不起。考虑创建一个新的主题。顺便说一句,如果我的回答已经解决了你的问题,请接受我的回答或微笑的回答。
private void frminventory_FormClosing(object sender, FormClosingEventArgs e) {
  // Data has not been changed, just close without pesky questions
  if (!isEdited)
    return;

  // If it's a user who is closing the form...
  if (e.CloseReason == CloseReason.UserClosing) {
    var action = MessageBox.Show(
      "You've edited the data, do you want to save it?"
       Text, // Let user know which form asks her/him
       MessageBoxButtons.YesNoCancel);

    if (DialogResult.Yes == action)
      Save(); // "Yes" - save the data edited and close the form
    else if (DialogResult.No == action)
      ;       // "No"  - discard the edit and close the form
    else
      e.Cancel = true; // "Cancel" - do not close the form (keep on editing) 
  }
}
    //Create this variable
    private bool _saved = true;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        DoSomething();
        _saved = true;
    }

    //Raised when the text is changed. I just put this for demonstration purpose.
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        _saved = false;
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (_saved == false)
        {
            var result = MessageBox.Show("Are you sure you want to close?\nYou may have unsaved information", "Information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            if (result == DialogResult.Yes)
            {
                _saved = true;
                Application.Exit();
            }
            else
                e.Cancel = true;
        }
    }