繁体   English   中英

如何验证用户输入?

[英]How do I validate user input?

所以我的代码有问题。 我需要验证用户输入的文本框中是否为数字。 现在,我可以查看它是否为数字,并且显示错误消息的信息很好,但是问题是,当我希望仅包含数字时,单词仍会输入到文本框中

如果尝试使用非IsNumeric(Number),则msgbox.show(“ ERROR!数据必须为数字!”)

  'Getting user input 
    Dim Number As String = Me.InputTextbox.Text
    UnitsTextbox.AppendText(Environment.NewLine & Number)

    'Make the textbox delete the text once the button is clicked
    InputTextbox.Text = String.Empty

    If Not IsNumeric(Number) Then
        MsgBox("ERROR! Data must be a number")
    End If

我希望它只接受数字

我有一个用于输入的文本框和一个用于结果的文本框,当数字为false时,我希望它不显示在结果文本框中

根据@Jens注释,只有我将其更改为.TryParse。 IsNumeric是一种旧的VB6方法,已在.Net中优化为TryParse。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim output As Integer
    If Not Integer.TryParse(InputTextbox.Text, output) Then
        MessageBox.Show("ERROR! Data must be a number")
    Else
        UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
        InputTextbox.Text = String.Empty
    End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM