Preventing a dialog box from closing in a button click event handler

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 the message field and prevent the dialog from closing. I do not know how to make the last part (preventing closure).

+52
c # windows winforms
Mar 23 '10 at 12:03
source share
10 answers

Given that you indicated that you need a default error dialog , one way to do this is to transfer your check to the OnClosing event handler. In this example, closing the form is interrupted if the user answers a question in the dialog box.

 private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // Determine if text has changed in the textbox by comparing to original text. if (textBox1.Text != strMyOriginalText) { // Display a MsgBox asking the user to save changes or abort. if(MessageBox.Show("Do you want to save changes to your text?", "My Application", MessageBoxButtons.YesNo) == DialogResult.Yes) { // Cancel the Closing event from closing the form. e.Cancel = true; // Call method to save file... } } } 

By setting e.Cancel = true , you cannot close the form.

However, it would be better to display inline validation errors (by highlighting visible fields, displaying tooltips, etc.) and prevent the user from choosing the OK button in the first place.

+36
Mar 23
source share

You can cancel the closure by setting the DialogResult form to DialogResult.None .

An example where button1 is an 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.

+120
Nov 04 '10 at 15:35
source share

Do not use the FormClosing event for this, you want to allow the user to reject the dialog, either cancel or click X. Just implement the OK button. Click the Event Handler and do not 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 something is wrong.

+12
Mar 23 '10 at 12:26
source share

This does not directly answer your question (others already exist), but from the point of view of ease of use, I would prefer the violation button to be disabled while the input is invalid.

+4
Mar 23 '10 at 12:13
source share

Use this code:

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

The problem is that the user has to double-click the buttons to close the forms;

+3
Sep 23 '10 at 14:18
source share

You can catch FormClosing to keep the form open. use the Cancel property for the event argument object for this.

 e.Cancel = true; 

and he should close your form.

+2
Mar 23 '10 at 12:10
source share

You can probably check the form before users click OK. If this is not an option, then open the message box, which says that something is wrong, and again open the form with the previous state.

0
Mar 23
source share

I wish I had time to find a better example, but you would be much better off using existing Windows form validation methods.

http://msdn.microsoft.com/en-us/library/ms229603.aspx

0
Mar 23 '10 at 12:25
source share

Just add one line to the event function

 private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { this->DialogResult = System::Windows::Forms::DialogResult::None; } 
0
May 05 '16 at 10:43
source share
 void SaveInfo() { blnCanCloseForm = false; Vosol[] vs = getAdd2DBVosol(); if (DGError.RowCount > 0) return; Thread myThread = new Thread(() => { this.Invoke((MethodInvoker)delegate { picLoad.Visible = true; lblProcces.Text = "Saving ..."; }); int intError = setAdd2DBVsosol(vs); Action action = (() => { if (intError > 0) { objVosolError = objVosolError.Where(c => c != null).ToArray(); DGError.DataSource = objVosolError;// dtErrorDup.DefaultView; DGError.Refresh(); DGError.Show(); lblMSG.Text = "Check Errors..."; } else { MessageBox.Show("Saved All Records..."); blnCanCloseForm = true; this.DialogResult = DialogResult.OK; this.Close(); } }); this.Invoke((MethodInvoker)delegate { picLoad.Visible = false; lblProcces.Text = ""; }); this.BeginInvoke(action); }); myThread.Start(); } void frmExcellImportInfo_FormClosing(object s, FormClosingEventArgs e) { if (!blnCanCloseForm) e.Cancel = true; } 
0
Jun 23 '17 at 8:02
source share



All Articles