Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey i'm very new to VB and have been attempting a question that requires me to display "fail" in the statusbox when two or more scores have a value less than 5, it seems to work for txtbox1 and txtbox2 but if i try any other textboxes then it comes up with an error.

"Conversion from string "" to type 'Double' is not valid"

i know this is real basic stuff but it's got me stuck

thanks in advance!
VB
If Not IsNumeric(TxtboxS1.Text & TxtboxS2.Text & TxtboxS3.Text) Then
    MessageBox.Show("Data not valid")
End If

If TxtboxS1.Text < 5 And (TxtboxS2.Text < 5) Then
    LblStatusBox.Text = "Fail"

ElseIf TxtboxS1.Text < 5 And (TxtboxS3.Text < 5) Then
    LblStatusBox.Text = "Fail"

ElseIf TxtboxS2.Text < 5 And (TxtboxS3.Text < 5) Then
    LblStatusBox.Text = "Fail"

Else : LblStatusBox.Text = "Pass"

End If
Posted
Updated 22-Mar-15 2:03am
v2

Look at your code:
VB
If Not IsNumeric(TxtboxS1.Text & TxtboxS2.Text & TxtboxS3.Text) Then
   MessageBox.Show("Data not valid")
End If
But you then continue as if it was all ok - so if I enter "Hello" in textbox 51 you will show me a message to say that's bad - and then continue to crash anyway!

Instead, use TryParse to convert the values:
VB
Dim s1 As Double, s2 As Double, s3__1 As Double
If Not (Double.TryParse(txtBoxS1.Text, s1) AndAlso Double.TryParse(txtBoxS2.Text, s2) AndAlso Double.TryParse(txtBoxS3.Text, s3__1)) Then
    MessageBox.Show("Data not valid")
Else
    If s1 < 5 AndAlso s2 < 5 Then
        LblStatusBox.Text = "Fail"
    ElseIf s1 < 5 AndAlso S3 < 5 Then
        ...
End If
 
Share this answer
 
That does not work like you did. You have to do the test for each and every textbox. Moreover, the logical AND operator in VB is AndAlso; but I think you need an OR operator here (OrElse in VB):
VB
If Not IsNumeric(Txtbox51.Text) OrElse Not IsNumeric(Txtbox52.Text) OrElse Not IsNumeric(Txtbox53.Text)
   ''
End If

This can be also expressed with an AND operator this way:
VB
If Not (IsNumeric(Txtbox51.Text) AndAlso IsNumeric(Txtbox52.Text) AndAlso IsNumeric(Txtbox53.Text))
   ''
EndIf


There remains to parse the text to double values; OriginalGriff explained that to you in Solution 1.
 
Share this answer
 
v2
For each of your TextBoxes you should validate data this way:
VB
Dim score As Long
If Long.TryParse(txbFirst.Text, score) Then
  If score < 5 Then
    LblStatusBox.Text = "Fail"
  End If
Else
  MessageBox.Show("Data not valid")
End If


Of course you'd better define just a Function to perform such validation and pass it the TextBoxes text iteratively (consider using an array of controls).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900