Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm validating a textbox control and want to "force" the user to enter valid info before moving on to the next control. I'm using e.Cancel=true in the validating event to accomplish this.

 

But, if I want the user to be able to cancel out of the form altogether (let's say press a cancel) button, how do I programatically get out of the validating event - with invalid text still in the text box?

 

TIA

Posted

Have you tried a different way of validating, like in a function?

If (Not IsNumeric(custDB.txtQuantity.Text)) Or custDB.txtQuantity.Text = "" Then
           MsgBox("Please enter Quantity (numbers only)", MsgBoxStyle.Exclamation)
             Exit Sub

ailzaj

Posted

OK, I found that you can use the "leave" event, check my "cancel" button for focus, then change the textbox "CausesValidation" property to false.

 

Thanks...

  • 1 month later...
Posted (edited)

I'm still trying to figure this one out. Two options so far:

 

1) Add a control that has the CausesValidation property set to false (Like a Cancel button). Close the form in that click event. Then do #2 below also.

 

or

 

2) Add a Closing event handler to the form, and set e.Cancel = false in the handler. Note, however, that the error message still displays before the form closes with this technique.

 

 

Just in case, I want to remind you that the "CausesValidation" property refers to the NEXT cotrol to get focus, and NOT to the control that is having the validating event.

 

Cheers!

 

David

Edited by anyoneis
Posted

I think you're thinking a tad to hard about this. :)

 

Use a form boolean variable IsValid, initialize it to true.

OnLeave TextBox Event:

Check for valid data, if valid set IsValid true, else false.

Closing Form Event:

If not IsValid, display error to user and cancel close.

Gamer extraordinaire. Programmer wannabe.
Posted

Are you suggesting that I skip the built in Validating event and roll my own scheme?

 

That is certainly doable, however the problem I am trying to solve is how to use the Validating event and the CausesValidation property as they were designed, BUT also to allow an operational Close box.

 

I think the ultimate right solution is for Microsoft to use the Form's CauseValidation property to handle the case of form level controls such as the close box getting focus. Then, we would have a way to get out of this mess cleanly.

 

David

Posted

Here is a more complete treatment of this issue from another newsgroup:

 

Amazing. This must mean that the Form.Close() function is causing the validating textbox to regain focus. And, contrary to what one might assume from reading the docs, the CausesValidation property CAN effectively be applied to the control in which it is defined, and not just some other (second) control which was previously in focus.

 

For what it's worth, you can use Validation and provide a clean form exit by

 

1) Add a Cancel button to your form

2) Set the Cancel button's CausesValidation property to false

3) Add a click event handler for the button, and in this handler set all controls' CausesValidation

properties to false. Then call this.Close(). As in:

------------

private void CancelButton_Click(object sender, System.EventArgs e)

{

minValueTextBox.CausesValidation = false;

maxValueTextBox.CausesValidation = false;

epMinTextBox.CausesValidation = false;

epMaxTextBox.CausesValidation = false;

 

this.Close();

}

------------

 

 

If you want the Close box on the title bar to be functional when there is a validation error, then you need to:

 

1) Add a Form Closing event handler, and in this handler set all controls' CausesValidation

properties to false. Then set e.Cancel = false and you are done. As in:

 

-----------

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)

{

minValueTextBox.CausesValidation = false;

maxValueTextBox.CausesValidation = false;

epMinTextBox.CausesValidation = false;

epMaxTextBox.CausesValidation = false;

 

e.Cancel = false;

}

 

-----------

 

If someone hits your Cancel button,you get a totally clean exit from the form. However, if the Close box on the title bar is pressed, they get a validation error THEN the form is closed.

 

Getting better than this will require a fix from MS or using Clay's suggestion to set all controls' CausesValidation properties to false when the WM_CLOSE message is detected.

 

Thanks Clay!

 

David

 

 

 

 

 

 

"ClayB [syncfusion]" <clayb@syncfusion.com> wrote in message news:%23R1gNFoLDHA.3392@tk2msftngp13.phx.gbl...

> One way you can do this is to override your form's WndProc method and catch

> the click on the System Menu's Close button, and then set the TextBox's

> CausesValidation property false.

>

> public const int SC_CLOSE = 0xF060;

> public const int WM_SYSCOMMAND = 0x0112;

> protected override void WndProc(ref System.Windows.Forms.Message m)

> {

> if(m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)

> {

> this.textBox1.CausesValidation = false;

> }

>

> base.WndProc(ref m);

> }

>

>

> ==================

> Clay Burch, .NET MVP

>

> Visit http://www.syncfusion.com for the coolest tools

>

>

Posted
Are you suggesting that I skip the built in Validating event and roll my own scheme?

 

That is certainly doable, however the problem I am trying to solve is how to use the Validating event and the CausesValidation property as they were designed, BUT also to allow an operational Close box.

 

I think the ultimate right solution is for Microsoft to use the Form's CauseValidation property to handle the case of form level controls such as the close box getting focus. Then, we would have a way to get out of this mess cleanly.

 

David

 

You can use the the Validating event from the Textbox if you want (instead of OnLeave), the principle is still the same. A single IsValid variable can be used to determine if the form is valid or not and checking it in the Closing event to determine if the form should be closed or not.

 

.. but you found a solution, so just ignore this. :p

Gamer extraordinaire. Programmer wannabe.
Posted
I got a load of grief trying to make use of the validation setup and using a Cancel button and got so bogged off with it I adopted a method similar to what wyrd suggests
My website

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...