在 vb.net 中使用函数“IsNumeric”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27003452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the function "IsNumeric" in vb.net
提问by larsvdb97
I have a question about using the IsNumeric()function in VB.NET.
I have to make a program that says if the value in a TextBox is numeric or not.
我有一个关于IsNumeric()在 VB.NET 中使用该函数的问题。
我必须制作一个程序来说明 TextBox 中的值是否为数字。
When I leave the TextBox, it has to display, in a Label, "your input is not numeric".
When I enter the TextBox, the text has to be deleted from the TextBox.
当我离开时TextBox,它必须在 a 中显示Label“您的输入不是数字”。
当我输入时TextBox,文本必须从TextBox.
I have no idea how to begin with this :/
Where do I have to place the code if I use a Windows Forms application and only have a closing button?
我不知道如何开始:/
如果我使用 Windows 窗体应用程序并且只有一个关闭按钮,我必须在哪里放置代码?
I have tried about everything I know.
我已经尝试了我所知道的一切。
回答by MPelletier
You want to setup your code in the "LeaveValidating" event of your textbox.
您想在文本框的“离开验证”事件中设置代码。
In Visual Studio, select your textbox, go to its properties, in the events (lightning bolt), go to "LeaveValidating", double-click it. This will bind the event and create a stub function. You want to use "IsNumeric" from there.
在 Visual Studio 中,选择您的文本框,转到其属性,在事件(闪电)中,转到“离开验证”,双击它。这将绑定事件并创建存根函数。您想从那里使用“IsNumeric”。
回答by Fredou
you have three choices
你有三个选择
depending on your need, i think validating would be one of the best one
根据您的需要,我认为验证将是最好的之一
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

