简体   繁体   English

关闭按钮无法正确关闭表单

[英]Close button is not closing the form properly

My form contains a button named as close . 我的表单包含一个名为closebutton I just put the below code in the close button click... Then the 2nd code set is for the form closing. 我只是将下面的代码放在关闭按钮中,单击...然后第二个代码集用于表单关闭。 But if I execute this when I click the close button a MessageBox will appear. 但是,如果我在单击close button时执行此操作,则会出现一个MessageBox When I click the "Yes" button, the form is not closing but if I click the "Yes" button second time the form will be closed. 当我单击“是”按钮时,该窗体未关闭,但是如果我第二次单击了“是”按钮,则该窗体将关闭。 What is the reason? 是什么原因? Can you, please, help me? 你能帮我么?

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();
    }
}

Do not close/exit Application , but Form : 不要关闭/退出Application ,而是关闭Form

private void btnClose_Click(object sender, EventArgs e) {
  // On btnClose button click all we should do is to Close the form
  Close();
}

private void frminventory_FormClosing(object sender, FormClosingEventArgs e) {
  // If it's a user who is closing the form...
  if (e.CloseReason == CloseReason.UserClosing) {
    // ...warn him/her and cancel form closing if necessary
    e.Cancel = MessageBox.Show("Are You Sure You Want To Close This Form?", 
                               "Close Application", 
                                MessageBoxButtons.YesNo) != DialogResult.Yes;
  }
}

Edit: Usually we ask direct questions to user (it's unclear what wrong things can arise if I just "Close This Form" ), eg 编辑:通常,我们向用户提出直接问题不清楚 ,如果我只是“关闭此表单” ,会发生什么错误的事情),例如

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) 
  }
}

So, because i can't comment, i would do something like this. 所以,因为我无法发表评论,所以我会做这样的事情。

    //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;
        }
    }

Hope this can solve yor problem 希望这可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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