2017-05-07 42 views
-1

Waht对此有错? 我有一个简单的形式与textbox1和textbox2和一个按钮。 当我把Textbox1 = 259和textbox2 = 1500 我点击,他说“你的号码太高” 我试试151“你的号码太高” 我试试150“ok” 我试试1500 “OK” 我尝试1501 “你数太高”vb net +检查Textbox1是否<Texbox2

帮助.........

Public Class test 

Private Sub test_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

End Sub 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    If TextBox1.Text > TextBox2.Text = True Then 
     MsgBox("You're number is too high") 
    Else 
     MsgBox("ok") 
    End If 

End Sub 

末级

+1

是你要比较的数值或字符串,看起来像数字时使用瓦尔(串)的功能。如果它是前者将字符串转换为数字并进行比较。还要除去If上的True。 – dbasnett

+0

您正在比较字符串。 “259”确实大于“1500”,“2”在“1”之后排序。 Val()和CDec()可用于进行转换。但考虑Decimal.TryParse(),还可以帮助您检查用户是否实际输入了一个数字,而不是“asdf”或根本没有。 –

回答

0

你必须使用Val命令。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
If Val(TextBox1.Text) > Val(TextBox2.Text) Then 
    MsgBox("You're number is too high", "Wrong Input") 
Else 
    MsgBox("ok", "Input number") ' This also renames the msgbox itself, instead of just "error". 
End If 
End Sub 
0
If Val(TextBox1.Text) > Val(TextBox2.Text) Then 
     MsgBox("You're number is too high") 
    Else 
     MsgBox("ok") 
    End If 

您需要将字符串转换为整数,双,小数,等