我有一个关于在VB.NET中使用IsNumeric()
函数的问题。
我必须制作一个程序,说明TextBox中的值是否为数字。
当我离开TextBox
时,必须在Label
中显示“您的输入不是数字”。
当我输入TextBox
时,必须从TextBox
删除文字。
我不知道如何开始:/
如果我使用Windows窗体应用程序并且只有一个关闭按钮,我在哪里放置代码?
我尝试了所有我知道的事情。
答案 0 :(得分:1)
您希望在文本框的“ Leave Validating”事件中设置代码。
在Visual Studio中,选择文本框,转到其属性,在事件(闪电)中,转到“保留验证”,然后双击它。这将绑定事件并创建存根函数。你想从那里使用“IsNumeric”。
答案 1 :(得分:1)
你有三个选择
根据您的需要,我认为验证将是最好的之一
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
If Not String.IsNullOrWhiteSpace(TextBox1.Text) AndAlso Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = ""
MessageBox.Show("Invalid text")
End If
End Sub
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
If Not String.IsNullOrWhiteSpace(TextBox1.Text) AndAlso Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = ""
MessageBox.Show("Invalid text")
End If
End Sub
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Not String.IsNullOrWhiteSpace(TextBox1.Text) AndAlso Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = ""
MessageBox.Show("Invalid text")
End If
End Sub