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

后端 未结 10 2017
你的背包
你的背包 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:52

    You can cancel closing by setting the Form's DialogResult to DialogResult.None.

    An example where button1 is the AcceptButton:

    private void button1_Click(object sender, EventArgs e) {
      if (!validate())
         this.DialogResult = DialogResult.None;
    }
    

    When the user clicks button1 and the validate method returns false, the form will not be closed.

提交回复
热议问题