I have a dialog that I show with <class>.ShowDialog()
. It has an OK button and a Cancel button; the OK button also has an event handler.
I want to do some input validation in the event handler and, if it fails, notify the user with a message box and prevent the dialog from closing. I don't know how to do the last part (preventing the close).
You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With