views:

268

answers:

8

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).

A: 

You can probably check the form before the users hits the OK button. If that's not an option, then open a message box saying something is wrong and re-open the form with the previous state.

Icono123
You can probably check the form before the users hits the OK button --> When? I can only check when they try to "submit" (borrowing the web's terminology) it, otherwise it will be invalid simply because the user hasn't had a chance to fill it in first. Your second suggestion is fairly difficult to implement; I was hoping for something simpler (or a different way to do what I'm trying to do)
qster
+6  A: 

One way of doing this is to move your validation into a OnClosing event handler. In this example the form close is a aborted if the user answers yes to the question in the dialog.

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 will prevent the form from closing.

ChrisF
that would work...but would be horrible UI practice. OP has already shown a dialog, you then throw other! Much better to use the existing winform validation techniques disable the 'OK' button
Adrian
@Adrian - I did say "one way" ;). It would all depend on how likely and/or serious the error was. I'd need to see the design of the form before making a final decision.
ChrisF
agreed and i've just read the OP again and saw he did want a HORRIBLE dialog to pop up!
Adrian
+1  A: 

You can catch FormClosing an there force the form to remain opened. use the Cancel property of the event argument object for that.

e.Cancel = true;

and it should stop your form from closing.

Adrian Faciu
+2  A: 

This doesn't directly answer your question (other already have), but from a usability point of view, I would prefer the offending button be disabled while the input is not valid.

Benjol
Well, yes, I thought of that but when a particular input contains an invalid value it's clearly marked with a red background.. I think it's enough even if disabling the button is trivial to implement, I guess, and no down sides to do it
qster
A: 

I wish I had time to find a better example, but you would be much better off using the existing windows forms validation techniques to do this.

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

Adrian
+3  A: 

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.

Hans Passant
A: 

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

The problem of it is that the user has to clic two times the buttons for closing the forms;

cigos emmanuel