Preventing a dialog from closing in the button's click event handler

后端 未结 10 1983
你的背包
你的背包 2020-12-29 19:40

I have a dialog that I show with .ShowDialog(). It has an OK button and a Cancel button; the OK button also has an event handler.

I want to

10条回答
  •  一生所求
    2020-12-29 19:43

    Don't use the FormClosing event for this, you'll want to allow the user to dismiss the dialog with either Cancel or clicking the X. Simply implement the OK button's Click event handler and don't close until you are happy:

    private void btnOk_Click(object sender, EventArgs e) {
      if (ValidateControls())
        this.DialogResult = DialogResult.OK;
    }
    

    Where "ValidateControls" is your validation logic. Return false if there's something wrong.

提交回复
热议问题