|
-
Aug 28th, 2007, 06:15 AM
#1
Thread Starter
Lively Member
Halt Validation
How do I halt validation on controls on a form if the user presses a cancel button on that form?
Private Sub Wizard1_CancelButtonClick(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Wizard1.CancelButtonClick
Try
If MessageBox.Show("You want to cancel?", "Cancel Wizard", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
Catch
MsgBox("There is an error")
End Try
End Sub
Trouble is is that I have a textbox on the form that validates user input
Private Sub Dont_Leave_empty(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NAMETextBox.Validating, ADDRESSTextBox.Validating
If Me.Table1BindingSource.Current IsNot Nothing And sender.Text = String.Empty Then
'Perform validation here.
MessageBox.Show(" You cannot leave this field blank", "Required Field", MessageBoxButtons.OK, MessageBoxIcon.Warning)
e.Cancel = True
Else
sender.Text = sender.Text.Trim()
End If
And this won't allow me to either cancel the user input or close the form
-
Aug 28th, 2007, 06:35 AM
#2
Re: Halt Validation
Try using a Form level Boolean. Something like blnIsCancelled
Insert, as the first line of your code
Code:
If blnIsCancelled Then Exit Sub
-
Aug 28th, 2007, 06:39 AM
#3
Thread Starter
Lively Member
Re: Halt Validation
Thats an idea - thanks hack
-
Aug 28th, 2007, 06:41 AM
#4
Re: Halt Validation
That is typically how I handle cancelling a looping process, but, it should work the same way for this situation.
-
Aug 28th, 2007, 06:48 AM
#5
Hyperactive Member
Re: Halt Validation
Set the CausesValidation property of the cancel button to false.
If my post helps , please feel free to rate it 
-
Aug 28th, 2007, 07:22 AM
#6
Thread Starter
Lively Member
Re: Halt Validation
Because e.cancel is set to true - it won't allow a boolean to be set when the cancel button is pressed
-
Aug 28th, 2007, 07:40 AM
#7
Re: Halt Validation
 Originally Posted by Moonshot
Because e.cancel is set to true - it won't allow a boolean to be set when the cancel button is pressed
He means in the designer. If you set a control's CausesValidation property to False then when that control receives focus the previous control's Validating and Validated events are not raised, which is exactly what you want.
-
Aug 28th, 2007, 10:13 AM
#8
Thread Starter
Lively Member
Re: Halt Validation
 Originally Posted by jmcilhinney
He means in the designer. If you set a control's CausesValidation property to False then when that control receives focus the previous control's Validating and Validated events are not raised, which is exactly what you want.
Yes but the causevalidation property is already set to false - the problem occurs because the textbox on which the form resides causes validation to fire if the field is left empty and the user tries to move to another textbox - FINE - this is exactly what I want to happen - EXCEPT - If I want to cancel the entire operation the cancel button on the forms events are not firing because the textbox validation code is running and because e.cancel is set to true until proper validation occurs the user cannot cancel or move on
-
Aug 28th, 2007, 10:14 AM
#9
Re: Halt Validation
Did you try the cancel boolean?
-
Aug 28th, 2007, 10:59 AM
#10
Thread Starter
Lively Member
Re: Halt Validation
 Originally Posted by Hack
Did you try the cancel boolean?
Yeah - wouldn't work either - it seems that validation will not let any other events fire until it is satisfied - I verified this using msgboxs
VB2005 express btw - maybe its a bug?
-
Aug 28th, 2007, 04:10 PM
#11
Hyperactive Member
Re: Halt Validation
I've tested it with this code....
vb Code:
Public Class Form1
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
e.Cancel = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If MessageBox.Show("Do you want to cancel", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
End Class
If I set the Cancel buttons CausesValidation to 'true' then I cannot close the form with the cancel button.
However if the CausesValidation is set to 'false' the form exits as expected.
So the fact that the textbox is validating is not the problem as the cancel button will prevent these events occuring when it's clicked.
If my post helps , please feel free to rate it 
-
Aug 28th, 2007, 05:50 PM
#12
Re: Halt Validation
Have you set the CausesValidation property of the Button to False or that of the TextBox? It is the BUTTON's CausesValidation property that needs to be set to False.
-
Aug 30th, 2007, 04:21 AM
#13
Thread Starter
Lively Member
Re: Halt Validation
 Originally Posted by jmcilhinney
Have you set the CausesValidation property of the Button to False or that of the TextBox? It is the BUTTON's CausesValidation property that needs to be set to False.
Yeah I have - I'm using Devcomponents Dotnetbar Wizard control - Perhaps its a problem with their control
-
Aug 30th, 2007, 05:59 AM
#14
Thread Starter
Lively Member
Re: Halt Validation
Have set every cause validation to false on the form / panels etc except two textboxes
Cancel button CAUSEVALIDATION set to False also
Still doesn't work - I'm at a loss here
-
Aug 30th, 2007, 06:30 AM
#15
Thread Starter
Lively Member
Re: Halt Validation
If I press the escape button on the keyboard it allows the cancel button code to operate
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|