Results 1 to 8 of 8

Thread: Validation Event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Validation Event

    I am developing an applicaiton that you can input auditing data to --> this updates a database and so on.

    What happens is the user enters an integer/double value into a textbox using an inkedit. The input looks like ink for a second and then it is recognized. If the value is too low or too high, the textbox changes color, red or green (if value is within spec). The value is only validated if another textbox is activated. My problem is when the last valu is added a user has to find something to click to validate the text! I want to eliminate that inconvenience. Right now the validation header is this:

    VB Code:
    1. Private Sub FBR1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FBR1.Validating

    I want to set it so it is validated when the text is recognized. Does that make sense??

    thx in advance!!

    Juri

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Validation Event

    I'm guessing that the TextChanged event will be raised when the ink is converted to text. The thing is, you have no guarantee that the user is going to enter the values in order. You'd have to use the TextChanged event for every control. You'd still want to use the Validating event though because it allows you to prevent the current control losing focus if its value is invalid. What usually happens is the last control is validated when the user clicks the OK button. If the control fails validation then it will not lose focus and the button click is not raised. You would usually have a Cancel button with its CausesValidation property set to False. This allows the user to click Cancel and exit without having the active control validated. Note that you can call the Validate method of the form at any time to raise the Validating on any controls that are awaiting validation.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Re: Validation Event

    Actually, now that I have thought about this for a little while. It would be nice if the form refreshed itself every second or so and every textbox with a validation event changed to reflect new data. For instance:

    Highgloss paint cannot have a gloss reading that is below 80
    Lowgloss paint can have any value.

    I have a radio button on the same form that allows the user to select either highgloss paint or lowgloss paint. If the user selects highgloss then enters into the textbox 65, then the box would turn red. Now say the user changes the selection to lowgloss paint, the textbox would validate this and turn green.
    Juri

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

    Re: Validation Event

    Quote Originally Posted by jmcilhinney
    Note that you can call the Validate method of the form at any time to raise the Validating on any controls that are awaiting validation.
    Note that you normally use the Validating event to prevent a control losing focus if it fails validation. If you do not intend to do that then it is more appropriate to use the Validated event. Calling Validate on a container control will raise the Validating then Validated event on every control that is awaiting validation.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Re: Validation Event

    ok I used both the TextChanged event and the validating event, works beautifully! Thank you!

    Juri

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

    Re: Validation Event

    Cool. Don't forget to resolve your thread from the Thread Tools menu.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    180

    Re: Validation Event

    One thing I noticed, the program crashes if the user enters an letter instead of a number - my code:

    VB Code:
    1. If (FBL1.Text = " ") Or FBL1.Text = String.Empty Then
    2.             FBL1.Text = "0"
    3.         End If
    4.         Dim DblFBL1 As Double = Convert.ToDouble(FBL1.Text)
    5.         Dim StrPaintType As String = PaintType.Text
    6.         If (StrPaintType = "EA") Then
    7.             If VirginRadio.Checked Then
    8.                 If (DblFBL1 < 2.0 Or DblFBL1 > 5.0) Then
    9.                     FBL1.BackColor = Color.Salmon
    10.                 Else
    11.                     FBL1.BackColor = Color.LightGreen
    12.                 End If
    13.             End If
    14.             If RepaintRadio.Checked Then
    15.                 FBL1.BackColor = Color.LightGreen
    16.             End If
    17.         End If
    18.         If (StrPaintType = "EB") Then
    19.             If VirginRadio.Checked Then
    20.                 If (DblFBL1 < 2.5 Or DblFBL1 > 5.0) Then
    21.                     FBL1.BackColor = Color.Salmon
    22.                 Else
    23.                     FBL1.BackColor = Color.LightGreen
    24.                 End If
    25.             End If
    26.             If RepaintRadio.Checked Then
    27.                 FBL1.BackColor = Color.LightGreen
    28.             End If
    29.         End If

    the crash happens at this line -

    Dim DblFBL1 As Double = Convert.ToDouble(FBL1.Text)

    Obviously because a letter cannot be converted into an integer, or in this cas a double. How can I prevent this? I have set the factoid for the box to only accept numerical inputs, however this is not 100%.

    TIA Juri

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

    Re: Validation Event

    Look into the Double.TryParse method.

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