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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)