Results 1 to 8 of 8

Thread: [Resolved][VB 2005] CauseValidation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Arrow [Resolved][VB 2005] CauseValidation

    Can you globally set the CauseValidation property of a textbox to False in a btnExit procedure?
    Last edited by Shodan; Oct 18th, 2006 at 11:00 PM.

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [VB 2005] CauseValidation

    Do you mean from another app?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Re: [VB 2005] CauseValidation

    No I mean I have 6 text boxes:

    txtRnr1Name.text
    txtRnr2Name.text
    txtRnr3Name.text

    txtRnr1Time.text
    txtRnr2Time.text
    txtRnr3Time.text

    Each one has a validating script.

    I want to be able at any time to exit the program. Right now I have to enter data into the boxes b4 I can exit.

    I want to be able to exit at any time and thought that I should turn CauseValidation to False in by btnExit procedure.


    Code:

    ##
    Public Class frmRace
    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

    'Declare my Variables
    Dim intRnr1Time As Integer
    Dim intRnr2Time As Integer
    Dim intRnr3Time As Integer

    'Assing the Variables
    intRnr1Time = CInt(txtRnr1Time.Text)
    intRnr2Time = CInt(txtRnr2Time.Text)
    intRnr3Time = CInt(txtRnr3Time.Text)

    'Calculate Runner One's Time
    If intRnr1Time < intRnr2Time And intRnr1Time < intRnr3Time Then
    lbl1stPlaceName.Text = txtRnr1Name.Text
    ElseIf intRnr1Time > intRnr2Time And intRnr1Time < intRnr3Time Or intRnr1Time < intRnr2Time And intRnr1Time > intRnr3Time Then
    lbl2ndPlaceName.Text = txtRnr1Name.Text
    Else
    lbl3rdPlaceName.Text = txtRnr1Name.Text
    End If

    'Calculate Runner Two's Time
    If intRnr2Time < intRnr1Time And intRnr2Time < intRnr3Time Then
    lbl1stPlaceName.Text = txtRnr2Name.Text
    ElseIf intRnr2Time > intRnr1Time And intRnr2Time < intRnr3Time Or intRnr2Time < intRnr1Time And intRnr2Time > intRnr3Time Then
    lbl2ndPlaceName.Text = txtRnr2Name.Text
    Else
    lbl3rdPlaceName.Text = txtRnr2Name.Text
    End If

    ''Calculate Runner Three's Time
    If intRnr3Time < intRnr1Time And intRnr3Time < intRnr2Time Then
    lbl1stPlaceName.Text = txtRnr3Name.Text
    ElseIf intRnr3Time > intRnr1Time And intRnr3Time < intRnr2Time Or intRnr3Time < intRnr1Time And intRnr3Time > intRnr2Time Then
    lbl2ndPlaceName.Text = txtRnr3Name.Text
    Else
    lbl3rdPlaceName.Text = txtRnr3Name.Text
    End If

    End Sub
    Private Sub txtRnr1Name_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr1Name.Validating

    If IsNumeric(txtRnr1Name.Text) Then
    MessageBox.Show("You can not enter a number")

    'Select existing text in the text box
    txtRnr1Name.SelectAll()
    e.Cancel = True

    ElseIf txtRnr1Name.Text = "" Then
    MsgBox("Please enter a Name")

    'Select existing text in the text box
    txtRnr1Name.SelectAll()

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr2Name_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr2Name.Validating

    If IsNumeric(txtRnr2Name.Text) Or txtRnr2Name.Text = "" Then
    MessageBox.Show("You can not enter a number")

    'Select existing text in the text box
    txtRnr2Name.SelectAll()
    e.Cancel = True

    ElseIf txtRnr2Name.Text = "" Then
    MsgBox("Please enter a Name")

    'Select existing text in the text box
    txtRnr1Name.SelectAll()

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr3Name_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr3Name.Validating

    If IsNumeric(txtRnr3Name.Text) Or txtRnr3Name.Text = "" Then
    MessageBox.Show("You can not enter a number")

    'Select existing text in the text box
    txtRnr3Name.SelectAll()
    e.Cancel = True

    ElseIf txtRnr3Name.Text = "" Then
    MsgBox("Please enter a Name")

    'Select existing text in the text box
    txtRnr1Name.SelectAll()

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr1Time_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr1Time.Validating

    If Not IsNumeric(txtRnr1Time.Text) Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)")

    ElseIf txtRnr1Time.Text = "" Then
    MsgBox("Please enter a Name")

    'Select existing text in the text box
    txtRnr1Time.SelectAll()

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr2Time_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr2Time.Validating

    If Not IsNumeric(txtRnr2Time.Text) Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)")

    ElseIf txtRnr2Time.Text = "" Then
    MsgBox("Please enter a Name")

    'Select existing text in the text box
    txtRnr2Time.SelectAll()

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr3Time_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr2Time.Validating

    If Not IsNumeric(txtRnr3Time.Text) Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)")

    ElseIf txtRnr3Time.Text = "" Then
    MsgBox("Please enter a Name")

    'Select existing text in the text box
    txtRnr3Time.SelectAll()

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Dim intResults As Integer

    txtRnr1Name.CausesValidation = False
    txtRnr2Name.CausesValidation = False
    txtRnr3Name.CausesValidation = False
    txtRnr1Time.CausesValidation = False
    txtRnr2Time.CausesValidation = False
    txtRnr3Time.CausesValidation = False

    intResults = MsgBox("Are Your Sure?", MsgBoxStyle.YesNo, "Cancel?")
    If intResults = Windows.Forms.DialogResult.Yes Then
    Me.Close()
    End If

    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    txtRnr1Name.Clear()
    txtRnr2Name.Clear()
    txtRnr3Name.Clear()
    txtRnr1Time.Clear()
    txtRnr2Time.Clear()
    txtRnr3Time.Clear()
    lbl1stPlaceName.Text = ""
    lbl2ndPlaceName.Text = ""
    lbl3rdPlaceName.Text = ""
    txtRnr1Name.Focus()

    End Sub

    End Class
    ##

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

    Re: [VB 2005] CauseValidation

    You don't set the CausesValidation property of the TextBoxes. You set the CausesValidation property of the Button. You have misuderstood what the CausesValidation property does
    Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus.
    If the CausesValidation property of a control is set to True then that means that when it receives focus any other controls that require validation will be validated. If it's False then they won't. That means that if you have a Cancel button you would set its CausesValidation property to False so that when the user clicks it no other controls will be validated, thus you can exit the form without validating the TextBoxes.
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Re: [VB 2005] CauseValidation



    Thanks for your help. I have Option Explicit & Option Strict set to on with Option Compare set to binary

    Is this why setting the btnExit.CauseValadation = True didn't work for me?

    I got the program to work but not the way you were suggesting.

    Code:

    ##
    Public Class frmRace
    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

    'Declare my Variables
    Dim intRnr1Time As Integer
    Dim intRnr2Time As Integer
    Dim intRnr3Time As Integer

    'Assing the Variables
    intRnr1Time = CInt(txtRnr1Time.Text)
    intRnr2Time = CInt(txtRnr2Time.Text)
    intRnr3Time = CInt(txtRnr3Time.Text)

    'Calculate Runner One's Time
    If intRnr1Time < intRnr2Time And intRnr1Time < intRnr3Time Then
    lbl1stPlaceName.Text = txtRnr1Name.Text
    ElseIf intRnr1Time > intRnr2Time And intRnr1Time < intRnr3Time Or intRnr1Time < intRnr2Time And intRnr1Time > intRnr3Time Then
    lbl2ndPlaceName.Text = txtRnr1Name.Text
    Else
    lbl3rdPlaceName.Text = txtRnr1Name.Text
    End If

    'Calculate Runner Two's Time
    If intRnr2Time < intRnr1Time And intRnr2Time < intRnr3Time Then
    lbl1stPlaceName.Text = txtRnr2Name.Text
    ElseIf intRnr2Time > intRnr1Time And intRnr2Time < intRnr3Time Or intRnr2Time < intRnr1Time And intRnr2Time > intRnr3Time Then
    lbl2ndPlaceName.Text = txtRnr2Name.Text
    Else
    lbl3rdPlaceName.Text = txtRnr2Name.Text
    End If

    ''Calculate Runner Three's Time
    If intRnr3Time < intRnr1Time And intRnr3Time < intRnr2Time Then
    lbl1stPlaceName.Text = txtRnr3Name.Text
    ElseIf intRnr3Time > intRnr1Time And intRnr3Time < intRnr2Time Or intRnr3Time < intRnr1Time And intRnr3Time > intRnr2Time Then
    lbl2ndPlaceName.Text = txtRnr3Name.Text
    Else
    lbl3rdPlaceName.Text = txtRnr3Name.Text
    End If

    End Sub
    Private Sub txtRnr1Name_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr1Name.Validating

    If IsNumeric(txtRnr1Name.Text) Then
    MessageBox.Show("You can NOT enter a Number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Select existing text in the text box
    txtRnr1Name.SelectAll()
    e.Cancel = True

    ElseIf txtRnr1Name.Text = "" Then
    MsgBox("Please enter a Name", MsgBoxStyle.Information, "Information")

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    'Set the e.Cancel to False to skip the validation
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr2Name_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr2Name.Validating

    If IsNumeric(txtRnr2Name.Text) Then
    MessageBox.Show("You can NOT enter a Number", "error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Select existing text in the text box
    txtRnr2Name.SelectAll()
    e.Cancel = True

    ElseIf txtRnr2Name.Text = "" Then
    MsgBox("Please enter a Name", MsgBoxStyle.Information, "Information")

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    'Set the e.Cancel to False to skip the validation
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr3Name_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr3Name.Validating

    If IsNumeric(txtRnr3Name.Text) Then
    MessageBox.Show("You can NOT enter a Number", "error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Select existing text in the text box
    txtRnr3Name.SelectAll()
    e.Cancel = True

    ElseIf txtRnr3Name.Text = "" Then
    MsgBox("Please enter a Name", MsgBoxStyle.Information, "Information")

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    'Set the e.Cancel to False to skip the validation
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr1Time_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr1Time.Validating

    If Not IsNumeric(txtRnr1Time.Text) Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Select existing text in the text box
    txtRnr1Time.SelectAll()
    e.Cancel = True

    ElseIf txtRnr1Time.Text = "" Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr2Time_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr2Time.Validating

    If Not IsNumeric(txtRnr2Time.Text) Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Select existing text in the text box
    txtRnr2Time.SelectAll()
    e.Cancel = True

    ElseIf txtRnr2Time.Text = "" Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub txtRnr3Time_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRnr3Time.Validating

    If Not IsNumeric(txtRnr3Time.Text) Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Select existing text in the text box
    txtRnr3Time.SelectAll()
    e.Cancel = True

    ElseIf txtRnr3Time.Text = "" Then
    MessageBox.Show("You must enter a number" & ControlChars.CrLf & _
    "(in seconds)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

    'Set the e.Cancel to True so the focus will stay
    e.Cancel = True
    Else
    e.Cancel = False
    End If

    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Dim intResults As Integer

    'Validates that the user wants to exit the program
    intResults = MessageBox.Show("Are you Sure?", "Exit Program", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If intResults = Windows.Forms.DialogResult.Yes Then

    'Set the CauseValidation back to False for all text boxes.
    'Setting the btnExit.causeValidation = False did not stop the validation process, Why??
    txtRnr1Name.CausesValidation = False
    txtRnr2Name.CausesValidation = False
    txtRnr3Name.CausesValidation = False

    txtRnr1Time.CausesValidation = False
    txtRnr2Time.CausesValidation = False
    txtRnr3Time.CausesValidation = False

    Me.Close()

    End If

    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    'clears all text boxes
    txtRnr1Name.Clear()
    txtRnr2Name.Clear()
    txtRnr3Name.Clear()
    txtRnr1Time.Clear()
    txtRnr2Time.Clear()
    txtRnr3Time.Clear()
    lbl1stPlaceName.Text = ""
    lbl2ndPlaceName.Text = ""
    lbl3rdPlaceName.Text = ""
    txtRnr1Name.Focus()

    End Sub

    End Class
    ##

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

    Re: [Resolved][VB 2005] CauseValidation

    No VB Options or any of that code makes any difference. In the designer set CausesValidation property of your Button to False. All the other controls should have their CausesValidation LEFT ALONE so it remains the default value of True. That's all you have to do. Change one property in the designer.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Re: [Resolved][VB 2005] CauseValidation

    Ok well that's weird cause I had set the causevalidation to false in the properties of btnExit and it didn't work. I tried to set it in the form1.vb module (code) in the btnExit procedure and that didn't work.

    You said designer, do you mean the form1.designer.vb??

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

    Re: [Resolved][VB 2005] CauseValidation

    OK, I see what's happening here. The problem is the fact that your form is not a dialogue and you're calling Close. It's not actually the Button that's causing the validation, but rather the call to Close. If your form was a dialogue, i.e. had been displayed by calling ShowDialog, then calling Close would not cause the form to be validated and it would work fine. As the form is not a dialogue, call Dispose instead of Close and it will work. I've never set CausesValidation to False on a form that wasn't a dialogue, so I've learned something too.
    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

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