|
-
May 1st, 2004, 04:34 PM
#1
Thread Starter
New Member
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:
Private mblngrade As Boolean
Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
'declare variables
Dim decTest1, decTest2, decTest3, decAverage As Decimal
Dim strID, strName, strGrade As String
Dim blnGrade As Boolean
'assign value to variables
decTest1 = Val(Me.Test1TextBox.Text)
decTest2 = Val(Me.Test2TextBox.Text)
decTest3 = Val(Me.Test3TextBox.Text)
strID = Me.IDTextBox.Text
strName = Me.StudentTextBox.Text
mblnGrade = ValidGrade(decTest1, decTest2, decTest3)
decAverage = (decTest1 + decTest2 + decTest3) / 3
'display Grade
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
End Sub
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
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
'clear the textboxes
Me.StudentTextBox.Text = ""
Me.IDTextBox.Text = ""
Me.Test1TextBox.Text = ""
Me.Test2TextBox.Text = ""
Me.Test3TextBox.Text = ""
Me.AverageDisplay.Text = ""
Me.GradeLabel.Text = ""
End Sub
'Validate TextBoxes and change forecolor to red if data is invalid
Private Sub Test1TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Test1TextBox.Validating, Test2TextBox.Validating, _
Test3TextBox.Validating
If mblngrade = True Then
e.Cancel = False
End If
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
End Class
-
May 1st, 2004, 06:19 PM
#2
PowerPoster
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.
-
May 1st, 2004, 06:40 PM
#3
Thread Starter
New Member
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:
Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) 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
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
-
May 1st, 2004, 07:08 PM
#4
PowerPoster
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:
Function ValidGrade(ByRef dec1 As Decimal, ByRef dec2 As Decimal, ByRef dec3 As Decimal) As Boolean
ValidGrade=False
If dec1 >= 0 And dec1 <= 100 Then
If dec2 >= 0 And dec2 <= 100 Then
If dec3 >= 0 And dec3 <= 100 Then
Return True
End If
End If
End If
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.
-
May 1st, 2004, 11:00 PM
#5
Thread Starter
New Member
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:
'declare form level variables
Private mdecTest1, mdecTest2, mdecTest3, mdecAverage As Decimal
Private mstrID, mstrName, mstrGrade As String
Private mblnGrade, mblnGrade2, mblnGrade3 As Boolean
Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
'assign value to variables
mdecTest1 = Val(Me.Test1TextBox.Text)
mdecTest2 = Val(Me.Test2TextBox.Text)
mdecTest3 = Val(Me.Test3TextBox.Text)
mstrID = Me.IDTextBox.Text
mstrName = Me.StudentTextBox.Text
mblnGrade = ValidGrade(mdecTest1)
mblnGrade2 = ValidGrade2(mdecTest2)
mblnGrade3 = ValidGrade3(mdecTest3)
mdecAverage = (mdecTest1 + mdecTest2 + mdecTest3) / 3
'display Grade
If mblnGrade = True Then
Me.AverageDisplay.Text = mdecAverage
Test1TextBox.ForeColor = Color.Black
Else
Test1TextBox.ForeColor = Color.Red
Me.AverageDisplay.Text = ""
End If
If mblnGrade2 = True Then
Me.AverageDisplay.Text = mdecAverage
Test2TextBox.ForeColor = Color.Black
Else
Test2TextBox.ForeColor = Color.Red
Me.AverageDisplay.Text = ""
End If
If mblnGrade3 = True Then
Me.AverageDisplay.Text = mdecAverage
Test3TextBox.ForeColor = Color.Black
Else
Test3TextBox.ForeColor = Color.Red()
Me.AverageDisplay.Text = ""
End If
End Sub
Function ValidGrade(ByRef dec1 As Decimal) As Boolean
If dec1 >= 0 And dec1 <= 100 Then
Return True
Else
Return False
End If
End Function
Function ValidGrade2(ByRef dec2 As Decimal) As Boolean
If dec2 >= 0 And dec2 <= 100 Then
Return True
Else
Return False
End If
End Function
Function ValidGrade3(ByRef dec3 As Decimal) As Boolean
If dec3 >= 0 And dec3 <= 100 Then
Return True
Else
Return False
End If
End Function
'clear the textboxes and send the focus to studenttextbox
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
Me.StudentTextBox.Text = ""
Me.IDTextBox.Text = ""
Me.Test1TextBox.Text = ""
Me.Test2TextBox.Text = ""
Me.Test3TextBox.Text = ""
Me.AverageDisplay.Text = ""
Me.GradeLabel.Text = ""
Me.StudentTextBox.Focus()
End Sub
'code the exit button
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
-
May 2nd, 2004, 08:05 AM
#6
PowerPoster
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:
Private Sub Test_Text_Validity(objTXT as TextBox)
if val(objTXT.Text)<=0 then objTXT.Forecolor=Color.Red
end sub
Private Sub Test1TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
Test_Text_Validity(Test1TextBox)
End Sub
Private Sub Test2TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
Test_Text_Validity(Test2TextBox)
End Sub
Private Sub Test3TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt1.LostFocus
Test_Text_Validity(Test3TextBox)
End Sub
(When you learn about Overloading, you can do away with two of the above Subs)
Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strMessage as String="Invalid Value in "
select case true
case val(Test1TextBox.text)<=0
strMessage=strMessage & "Test1TextBox"
case val(Test2TextBox.text)<=0
strMessage=strMessage & "Test2TextBox"
case val(Test3TextBox.text)<=0
strMessage=strMessage & "Test3TextBox"
case else
mstrName = Me.StudentTextBox.Text
AverageDisplay.Text=Str(= (val(Test1TextBox.text) +val(Test2TextBox.text)+ val(Test1TextBox.text)) / 3)
Exit Sub
End select
Messagebox.show(strMessage)
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
StudentTextBox.Text = ""
IDTextBox.Text = ""
Test1TextBox.Text = ""
Test1TextBox.ForeColor=Color.Black
Test2TextBox.Text = ""
Test2TextBox.ForeColor=Color.Black
Test3TextBox.Text = ""
Test3TextBox.ForeColor=Color.Black
AverageDisplay.Text = ""
GradeLabel.Text = ""
StudentTextBox.Focus()
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.
-
May 2nd, 2004, 08:59 AM
#7
Thread Starter
New Member
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
-
May 2nd, 2004, 02:07 PM
#8
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|