VB.Net VS 2008验证textbox.text仅为一个小数点

VB.Net VS 2008验证textbox.text仅为一个小数点,vb.net,Vb.net,尊敬的先生, 我限制我的文本框仅用于数字和小数点,我只能通过下面的函数获取数字和小数,但不能限制小数点在输入文本框上出现两次。我猜如果singleChars.IndexOf(KeyChar)>0和(Asc(KeyChar))8有一些错误,如果它是错误的,如何解决它 Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text

尊敬的先生, 我限制我的文本框仅用于数字和小数点,我只能通过下面的函数获取数字和小数,但不能限制小数点在输入文本框上出现两次。我猜
如果singleChars.IndexOf(KeyChar)>0和(Asc(KeyChar))8有一些错误,如果它是错误的,如何解决它

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = myClasses.onlyCurrency(e.KeyChar)
    End Sub
类文件中的公共函数是

Public Shared Function onlyCurrency(ByVal KeyChar As Char) As Boolean
        Dim allowedChars As String
        allowedChars = "0123456789."
        Dim singleChars As String
        singleChars = "."
        If allowedChars.IndexOf(KeyChar) = -1 And (Asc(KeyChar)) <> 8 Then
            Return True
        End If
        If singleChars.IndexOf(KeyChar) > 0 And (Asc(KeyChar)) <> 8 Then
            Return True
        End If
        Return False
    End Function
公共共享函数onlyCurrency(ByVal KeyChar作为Char)作为布尔值
暗允许字符作为字符串
allowedChars=“0123456789。”
暗单字符作为字符串
singleChars=“”
如果允许chars.IndexOf(KeyChar)=-1和(Asc(KeyChar))8,则
返回真值
如果结束
如果singleChars.IndexOf(KeyChar)>0和(Asc(KeyChar))8,那么
返回真值
如果结束
返回错误
端函数
你忠实的 Murulimadhav

像这样试试

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As 
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If Char.IsDigit(e.KeyChar) = False AndAlso e.KeyChar <> "." Then
        e.Handled = True
    ElseIf e.KeyChar = "." AndAlso TextBox1.Text.Trim.Contains(".") Then
        e.Handled = True
    Else
        e.Handled = False
    End If

End Sub
Private Sub TextBox1\u按键(ByVal sender作为System.Object,ByVal e作为System.Object
System.Windows.Forms.KeyPressEventArgs)处理TextBox1.KeyPress
如果Char.IsDigit(e.KeyChar)=False,也可以是e.KeyChar“。”
e、 已处理=真
ElseIf e.KeyChar=“.”AndAlso TextBox1.Text.Trim.Contains(“.”)然后
e、 已处理=真
其他的
e、 已处理=错误
如果结束
端接头

您没有注意文本框中已经输入的内容,因此您的函数不知道输入了多少小数点。您需要将文本框的文本传递到函数中,或者在发送到函数之前对其进行预筛选。大概是这样的:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    e.Handled = myClasses.onlyCurrency(e.KeyChar, CType(sender, TextBox).Text)

End Sub



Public Shared Function onlyCurrency(ByVal KeyChar As Char, CurrentText As String) As Boolean
    Dim allowedChars As String
    allowedChars = "0123456789."
    Dim singleChars As String
    singleChars = "."
    If KeyChar = singleChars Then
        If CurrentText.Contains(singleChars) Then
            Return True
        End If
    End If
    If allowedChars.IndexOf(KeyChar) = -1 And (Asc(KeyChar)) <> 8 Then
        Return True
    End If
    Return False
End Function
Private Sub TextBox1\u KeyPress(发送者作为对象,e作为KeyPressEventArgs)处理TextBox1.KeyPress
e、 Handled=myClasses.onlyCurrency(e.KeyChar,CType(sender,TextBox).Text)
端接头
公共共享函数onlyCurrency(ByVal KeyChar作为字符,CurrentText作为字符串)作为布尔值
暗允许字符作为字符串
allowedChars=“0123456789。”
暗单字符作为字符串
singleChars=“”
如果KeyChar=singleChars,则
如果CurrentText.Contains(单字符)则
返回真值
如果结束
如果结束
如果允许chars.IndexOf(KeyChar)=-1和(Asc(KeyChar))8,则
返回真值
如果结束
返回错误
端函数

尊敬的先生,是的,现在收到了,先生。我没有试图读取文本框“If CurrentText.Contains(singleChars)”中输入的内容。谢谢,先生。您诚挚的Murulimadhave欢迎您,很高兴能为您提供帮助。