“从字符串转换”"输入'Double'无效“ [英] "Conversion from string "" to type 'Double' is not valid"
问题描述
嘿,我是VB的新手,并且当两个或多个分数的值小于5时,我一直在尝试一个问题,要求我在状态框中显示失败,它似乎适用于txtbox1和txtbox2但是如果我尝试任何其他文本框然后它会出现错误。
从字符串转换为'Double'类型无效
i知道这是真正的基本东西,但它让我卡住了
提前感谢!
如果 不 IsNumeric(TxtboxS1.Text& TxtboxS2.Text& TxtboxS3.Text)然后
MessageBox.Show( 数据无效)
结束 如果
如果 TxtboxS1.Text< 5 和(TxtboxS2.Text< 5 )然后
LblStatusBox.Text = 失败
ElseIf TxtboxS1.Text< 5 和(TxtboxS3.Text< 5 )然后
LblStatusBox.Text = 失败
ElseIf TxtboxS2.Text< 5 和(TxtboxS3.Text< 5 )然后
LblStatusBox.Text = 失败
否则:LblStatusBox.Text = < span class =code-string>传递
结束 如果
看看你的代码:
如果 不 IsNumeric(TxtboxS1.Text& TxtboxS2.Text& TxtboxS3.Text)然后
MessageBox.Show( 数据无效 )
结束 如果但是你继续作为如果一切都好 - 所以如果我在文本框51中输入你好,你会告诉我一条消息说这很糟糕 - 然后继续崩溃!
相反,使用TryParse转换值:
Dim s1 正如 Double ,s2 As Double ,s3__1 作为 Double
如果 不( Double .TryParse(txtBoxS1.Text,s1) AndAlso Double .TryParse(txtBoxS2.Text,s2) AndAlso Double .TryParse(txtBoxS3.Text,s3__1))然后
MessageBox.Show( 数据无效)
否则
如果 s1< 5 AndAlso s2< 5 然后
LblStatusBox.Text = 失败
ElseIf s1< 5 AndAlso S3< 5 然后
...
结束 如果
这不像你那样工作。您必须为每个文本框进行测试。而且,VB中的逻辑AND运算符是AndAlso
;但我认为你需要一个OR运算符(VB中的OrElse
):
如果 不 IsNumeric(Txtbox51.Text) OrElse 不 IsNumeric(Txtbox52.Text) OrElse 不 IsNumeric(Txtbox53。文字)
' '
结束 如果
这也可以用AND运算符表示:
如果 不(IsNumeric(Txtbox51.Text) AndAlso IsNumeric(Txtbox52.Text) AndAlso IsNumeric(Txtbox53.Text))
' '
EndIf
仍然需要将文本解析为double值; OriginalGriff在解决方案1中向您解释。
对于每个TextBox,您应该以这种方式验证数据:
Dim 得分为 长
如果 长 .TryParse(txbFirst.Text,得分)那么
如果得分< 5 然后
LblStatusBox.Text = 失败
结束 如果
Else
MessageBox.Show( 数据无效)
结束 如果
当然你最好定义一个函数来执行这样的验证并迭代地传递TextBoxes文本(考虑使用一组控件)
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!
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
Look at your code:
If Not IsNumeric(TxtboxS1.Text & TxtboxS2.Text & TxtboxS3.Text) Then MessageBox.Show("Data not valid") End IfBut 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:
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
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 isAndAlso
; but I think you need an OR operator here (OrElse
in 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:
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.
For each of your TextBoxes you should validate data this way:
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).
这篇关于“从字符串转换”"输入'Double'无效“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!