Results 1 to 15 of 15

Thread: Halt Validation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    Re: Halt Validation

    Thats an idea - thanks hack

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Halt Validation

    That is typically how I handle cancelling a looping process, but, it should work the same way for this situation.

  5. #5
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Halt Validation

    Set the CausesValidation property of the cancel button to false.
    If my post helps , please feel free to rate it

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Halt Validation

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    Re: Halt Validation

    Quote 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

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Halt Validation

    Did you try the cancel boolean?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    Re: Halt Validation

    Quote 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?

  11. #11
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Halt Validation

    I've tested it with this code....

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    4.         e.Cancel = True
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         If MessageBox.Show("Do you want to cancel", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
    9.             Me.Close()
    10.         End If
    11.  
    12.     End Sub
    13. 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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    Re: Halt Validation

    Quote 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

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    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

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    124

    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
  •  



Click Here to Expand Forum to Full Width