How can I make prevent the Validate event from going off when the user hits the Close button (the X

  • Thread starter Thread starter 0to60
  • Start date Start date
0

0to60

I have, for example, a combobox on a form. If the user types in some
nonexistant customer into the combobox and leaves the field, I have the
Validate event notify the user of the bad customer and return the focus to
the form. Pretty basic stuff, and its all running nicely. Now, I would
like the user to be able to hit the X button (or select "close" from the
system menu) but NOT have to deal with the validation code. Like, maybe
they just want to get out of the form without further ado. I have tried
everything under the sun, but Validate is not to be denied!!

I've tried messing around with comboBox.CausesValidation and
form.CausesValidation, but it didn't work.
I overloaded WndProc and handled the WM_CLOSE message by setting
comboBox.CausesValidation = false but to no avail.
I overloaded WndProc and handled WM_SYSCOMMAND where wParam = SC_CLOSE and
set comboBox.CausesValidation = false but no dice.
I overloaded WndProc and handled WM_NCLBUTTONDOWN where wParam = 20 (that's
the user clicking on the X button) and setting comboBox.CausesValidation =
false, it did absolutely nothing.

Is there any way to bypass validation when the user closes the form? It
seems like signing up for Validation is a deal with devil. There's no
getting out of it.
 
You might be thinking about the CausesValidation parameter incorrectly.
This parameter is in effect when the user moves away from the combo box.
e.g. if you have a simple dialog box with a combo box and an OK and
Cancel button, the OK button would have the CausesValidation parameter
set to true and the Cancel button should have the CausesValidation
parameter set to false...

Ummm...Just found another ugly detail. The Close event actually
triggers the validation event. So, as an alternative you can call
Dispose instead of Close and all should be well. Calling Dispose in
your form's closing event seemed to work for me...

Hope that helps,
Scott
 
I'm having a similar issue, except with a row validating event in a datagridview. If the required cells have not been populated, I prevent the user from advancing to the next row. Of course, if during a row edit session the user decides to press the form close button, my row validating handler was firing and cancelling that event which, in turn, caused the form not to be closed (even though the form_closing event was fired). Since the row_validating event fired before the form_closing event, I couldn't see a way to determine from within my validating sub if the user had requested the form to be closed. However, after some Googling, I determined that I needed a "me.dispose" at the end of my form_closing handler (which I've never used or needed before). This seems to work without any adverse effects. Not sure if this is an acceptable technique, but for now it seems to work.

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Well, I overlooked one of your issues. If you are displaying a message box or dialog (which I'm not), that's an entirely different problem. I saw a lot of hoakie-looking solutions out there, but the one that may help you is to check he location of the mouse when your validation occurs. If it is in the vicinity of the close box, you can abort the validation. Here's where I saw that tip:

http://windowsclient.net/blogs/faqs...nt-when-the-user-clicks-on-the-close-box.aspx

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Well, I overlooked one of your issues. If you are displaying a message box or dialog (which I'm not), that's an entirely different problem. I saw a lot of hoakie-looking solutions out there, but the one that may help you is to check he location of the mouse when your validation occurs. If it is in the vicinity of the close box, you can abort the validation. Here's where I saw that tip:

http://windowsclient.net/blogs/faqs...nt-when-the-user-clicks-on-the-close-box.aspx

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
The best and easy solution could be:
1.declare a bool variable and set false.
private bool tabKeyPressed = false;

2.On key down event of datagrid1 set true:

private void dataGridView1_KeyDown(object sender, KeyEventArgs
{
if (e.KeyData == Keys.Tab)
{
tabKeyPressed = true;
}
}

3. run validation if value is true and after finishing all the
rowvalidating task set it to false.

private void dataGridView1_RowValidating(object sender,
DataGridViewCellCancelEventArgs e)
{
if(tabKeyPressed==true)
{
MessageBox.Show("hello");
//do your validation
}
tabKeyPressed = false;
}
 
Back
Top