Results 1 to 8 of 8

Thread: Validating Event VB.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7

    Validating Event VB.net

    Alright, I have read and searched but I dont understand what a validating event is and how I use it for this program. Basically, I am trying to make sure that text in 3 dif textboxes is btween 0 and 100 and if its not, I need to change the forecolor to red . Can someone let me know if I am on the right track? My prob. right now is changing the forecolor.
    Thanks
    Jen

    VB Code:
    1. Private mblngrade As Boolean
    2.     Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
    3.         'declare variables
    4.         Dim decTest1, decTest2, decTest3, decAverage As Decimal
    5.         Dim strID, strName, strGrade As String
    6.         Dim blnGrade As Boolean
    7.         'assign value to variables
    8.         decTest1 = Val(Me.Test1TextBox.Text)
    9.         decTest2 = Val(Me.Test2TextBox.Text)
    10.         decTest3 = Val(Me.Test3TextBox.Text)
    11.  
    12.         strID = Me.IDTextBox.Text
    13.         strName = Me.StudentTextBox.Text
    14.         mblnGrade = ValidGrade(decTest1, decTest2, decTest3)
    15.         decAverage = (decTest1 + decTest2 + decTest3) / 3
    16.         'display Grade
    17.  
    18.         If blnGrade = True Then
    19.             Me.AverageDisplay.Text = decAverage
    20.         Else
    21.             MessageBox.Show("This value must be between 0 and 100.", "Average", _
    22.             MessageBoxButtons.OK, MessageBoxIcon.Information)
    23.         End If
    24.     End Sub
    25.  
    26.     Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) As Boolean
    27.         'delcare varaible for boolean value
    28.         Dim blnGrade As Boolean
    29.  
    30.         If dec1 >= 0 And dec1 <= 100 Then
    31.             If dec2 >= 0 And dec2 <= 100 Then
    32.                 If dec3 >= 0 And dec3 <= 100 Then
    33.                     Return True
    34.                 Else : Return False
    35.                 End If
    36.             End If
    37.         End If
    38.  
    39.     End Function
    40.  
    41.  
    42.  
    43.  
    44.  
    45.     Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
    46.         'clear the textboxes
    47.         Me.StudentTextBox.Text = ""
    48.         Me.IDTextBox.Text = ""
    49.         Me.Test1TextBox.Text = ""
    50.         Me.Test2TextBox.Text = ""
    51.         Me.Test3TextBox.Text = ""
    52.         Me.AverageDisplay.Text = ""
    53.         Me.GradeLabel.Text = ""
    54.     End Sub
    55.    
    56.     'Validate TextBoxes and change forecolor to red if data is invalid
    57.     Private Sub Test1TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Test1TextBox.Validating, Test2TextBox.Validating, _
    58.     Test3TextBox.Validating
    59.         If mblngrade = True Then
    60.             e.Cancel = False
    61.         End If
    62.     End Sub
    63.  
    64.     Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
    65.         Me.Close()
    66.     End Sub
    67. End Class

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "Alright, I have read and searched but I dont understand what a validating event is and how I use it for this program"

    You are already using a validating event in this post AND in your previous post.

    i.e.

    " If blnGrade = True Then
    Me.AverageDisplay.Text = decAverage
    Else
    MessageBox.Show("This value must be between 0 and 100.", "Average", _
    MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If"

    You are simply checking the value of the entries made and ensuring they are between 0 and 100 - i.e. You are trying to VALIDATE those entries. (Even though you have failed to do so - see below)




    In your following code

    " Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) As Boolean
    'delcare varaible for boolean value
    Dim blnGrade As Boolean

    If dec1 >= 0 And dec1 <= 100 Then
    If dec2 >= 0 And dec2 <= 100 Then
    If dec3 >= 0 And dec3 <= 100 Then
    Return True
    Else : Return False
    End If
    End If
    End If

    End Function"

    You do not need to declare blnGrade as Boolean. The function returns the function result under the name of the function. In your case blnGrade (please note the possible mis-spelling in your code as you have not declared mblnGrade) will be set to the result of the function ValidGrade. You have, in fact, tried to declare blnGrade in two separate subs!!

    Also, the function will only return False if dec3 is false. If either dec1 or dec2 are incorrect, the function will show them as correct.

    Have a look at the following thread and let us know if it solves your problem:

    http://www.vbforums.com/showthread.p...&highlight=red
    Last edited by taxes; May 1st, 2004 at 06:26 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7
    Hi, thanks for the reply!

    This function returns a message box stating an error for dec1, dec2, and dec 3 (any of them) That part works fine.

    VB Code:
    1. Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) As Boolean
    2.         If dec1 >= 0 And dec1 <= 100 Then
    3.             If dec2 >= 0 And dec2 <= 100 Then
    4.                 If dec3 >= 0 And dec3 <= 100 Then
    5.                     Return True
    6.                 Else : Return False
    7.                 End If
    8.             End If
    9.         End If

    What I dont understand is ... the validating event.... and changing the forecolor to red when the function returns a false value to the click event sub procedure.
    Thanks
    Jen

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "This function returns a message box stating an error for dec1, dec2, and dec 3 (any of them) That part works fine."


    When dec1 is less than 0, the function code you posted exits on the line

    If dec1 >= 0 And dec1 <= 100 Then

    with the result that Return False does NOT get executed. OIf you are getting a False result for dec1 or dec2, then your other code must be producing it.

    If you want to use that code then try

    VB Code:
    1. Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) As Boolean
    2.      ValidGrade=False
    3.         If dec1 >= 0 And dec1 <= 100 Then
    4.             If dec2 >= 0 And dec2 <= 100 Then
    5.                 If dec3 >= 0 And dec3 <= 100 Then
    6.                     Return True
    7.                  End If
    8.             End If
    9.         End If
    10.  
    11.     End Function
    Now, unless all three are OK the function will return False.


    "What I dont understand is ... the validating event.... and changing the forecolor to red when the function returns a false value to the click event sub procedure."

    I covered this fully. Which part of my response did you not understand?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7
    Should my Function Procedure send the value back to the CalcButton Click event procedure or should I send it somewhere else?

    I am very new at this and this program is obviously way beyond my skill level.. but I am just trying to learn as much as I can.
    This is my revised code.. not that its any better

    jen

    VB Code:
    1. 'declare form level variables
    2.     Private mdecTest1, mdecTest2, mdecTest3, mdecAverage As Decimal
    3.     Private mstrID, mstrName, mstrGrade As String
    4.     Private mblnGrade, mblnGrade2, mblnGrade3 As Boolean
    5.  
    6.     Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
    7.         'assign value to variables
    8.         mdecTest1 = Val(Me.Test1TextBox.Text)
    9.         mdecTest2 = Val(Me.Test2TextBox.Text)
    10.         mdecTest3 = Val(Me.Test3TextBox.Text)
    11.         mstrID = Me.IDTextBox.Text
    12.         mstrName = Me.StudentTextBox.Text
    13.         mblnGrade = ValidGrade(mdecTest1)
    14.         mblnGrade2 = ValidGrade2(mdecTest2)
    15.         mblnGrade3 = ValidGrade3(mdecTest3)
    16.         mdecAverage = (mdecTest1 + mdecTest2 + mdecTest3) / 3
    17.  
    18.         'display Grade
    19.         If mblnGrade = True Then
    20.             Me.AverageDisplay.Text = mdecAverage
    21.             Test1TextBox.ForeColor = Color.Black
    22.         Else
    23.             Test1TextBox.ForeColor = Color.Red
    24.             Me.AverageDisplay.Text = ""
    25.         End If
    26.  
    27.         If mblnGrade2 = True Then
    28.             Me.AverageDisplay.Text = mdecAverage
    29.             Test2TextBox.ForeColor = Color.Black
    30.         Else
    31.             Test2TextBox.ForeColor = Color.Red
    32.             Me.AverageDisplay.Text = ""
    33.         End If
    34.  
    35.         If mblnGrade3 = True Then
    36.             Me.AverageDisplay.Text = mdecAverage
    37.             Test3TextBox.ForeColor = Color.Black
    38.         Else
    39.             Test3TextBox.ForeColor = Color.Red()
    40.             Me.AverageDisplay.Text = ""
    41.         End If
    42.  
    43.     End Sub
    44.  
    45.     Function ValidGrade(ByRef dec1 As Decimal) As Boolean
    46.         If dec1 >= 0 And dec1 <= 100 Then
    47.             Return True
    48.         Else
    49.             Return False
    50.         End If
    51.     End Function
    52.  
    53.     Function ValidGrade2(ByRef dec2 As Decimal) As Boolean
    54.         If dec2 >= 0 And dec2 <= 100 Then
    55.             Return True
    56.         Else
    57.             Return False
    58.         End If
    59.     End Function
    60.  
    61.     Function ValidGrade3(ByRef dec3 As Decimal) As Boolean
    62.         If dec3 >= 0 And dec3 <= 100 Then
    63.             Return True
    64.         Else
    65.             Return False
    66.         End If
    67.     End Function
    68.  
    69.     'clear the textboxes and send the focus to studenttextbox
    70.     Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
    71.         Me.StudentTextBox.Text = ""
    72.         Me.IDTextBox.Text = ""
    73.         Me.Test1TextBox.Text = ""
    74.         Me.Test2TextBox.Text = ""
    75.         Me.Test3TextBox.Text = ""
    76.         Me.AverageDisplay.Text = ""
    77.         Me.GradeLabel.Text = ""
    78.         Me.StudentTextBox.Focus()
    79.     End Sub
    80.  
    81.     'code the exit button
    82.     Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
    83.         Me.Close()
    84.     End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    From your posts, you seem to be ignoring my replies/questions.
    The thread I quoted covers exactly the same problem as you are raising. Have you looked at it? If you have, you should be using something like:

    VB Code:
    1. Private Sub Test_Text_Validity(objTXT as TextBox)
    2.    if val(objTXT.Text)<=0 then objTXT.Forecolor=Color.Red
    3. end sub
    4.  
    5. Private Sub Test1TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
    6.     Test_Text_Validity(Test1TextBox)
    7.  End Sub
    8.  
    9. Private Sub Test2TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
    10.     Test_Text_Validity(Test2TextBox)
    11.  End Sub
    12.  
    13. Private Sub Test3TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
    14.     Test_Text_Validity(Test3TextBox)
    15.  End Sub
    16.  
    17. (When you learn about Overloading,  you can do away with two of the above Subs)
    18.  
    19.  
    20.     Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    21.         Dim strMessage as String="Invalid Value in "
    22.         select case true
    23.             case val(Test1TextBox.text)<=0
    24.                 strMessage=strMessage & "Test1TextBox"
    25.             case val(Test2TextBox.text)<=0
    26.                 strMessage=strMessage & "Test2TextBox"
    27.             case val(Test3TextBox.text)<=0
    28.                 strMessage=strMessage & "Test3TextBox"
    29.             case else
    30.                 mstrName = Me.StudentTextBox.Text
    31.  AverageDisplay.Text=Str(= (val(Test1TextBox.text) +val(Test2TextBox.text)+ val(Test1TextBox.text)) / 3)
    32.  
    33.                 Exit Sub
    34.          End select
    35.          Messagebox.show(strMessage)
    36.         End Sub
    37.  
    38. Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
    39.         StudentTextBox.Text = ""
    40.         IDTextBox.Text = ""
    41.         Test1TextBox.Text = ""
    42.         Test1TextBox.ForeColor=Color.Black
    43.         Test2TextBox.Text = ""
    44.         Test2TextBox.ForeColor=Color.Black
    45.         Test3TextBox.Text = ""
    46.         Test3TextBox.ForeColor=Color.Black
    47.         AverageDisplay.Text = ""
    48.         GradeLabel.Text = ""
    49.         StudentTextBox.Focus()
    50.     End Sub

    I have not checked this for bugs.
    Last edited by taxes; May 2nd, 2004 at 08:10 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    7
    I have NOT ignored your posts. I am not looking for someone to write the code for me, I just wanted some help understanding the whole concept. Thanks for your help, but nevermind, I will just figure it out myself.

    Thanks

    Jennifer

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi jenbug,

    I did not intend to offend. Please accept my apologies. I was just trying to determine exactly what it was that you did not understand.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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