简体   繁体   English

允许用户在文本框 VB.NET 2010 中只写一个点和数字

[英]Allow user to write only one dot And only numbers in a textbox VB.NET 2010

I have a textbox and I am using the following to validate the user can only add numbers, but how can I let the user to write only one "."我有一个文本框,我正在使用以下内容来验证用户只能添加数字,但我怎样才能让用户只写一个“。”

Private Sub txtDiagnostic_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtConsultor.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then
            Else
                e.Handled = True
            End If
        End If
    End Sub

I was planing to use contains() or something but how can I realize the textbox has only one dot ?我打算使用 contains() 或其他东西,但我怎么能意识到文本框只有一个点 or how to allow the user can write a single dot and only one?或者如何让用户可以写一个点并且只能写一个?

add this line before your first IF在您的第一个IF之前添加此行

If (e.KeyChar.ToString = ".") And (txtDiagnostic.Text.Contains(e.KeyChar.ToString)) Then
     e.Handled = True
     Exit Sub
End If

try this code good for money purposes试试这个代码对金钱有好处

    Private Sub LoanFeeTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles LoanFeeTextBox.KeyPress
        If Char.IsControl(e.KeyChar) Then
        ElseIf Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "."c Then
            If LoanFeeTextBox.TextLength = 12 And LoanFeeTextBox.Text.Contains(".") = False Then
                LoanFeeTextBox.AppendText(".")
            ElseIf e.KeyChar = "." And LoanFeeTextBox.Text.IndexOf(".") <> -1 Then
                e.Handled = True
            ElseIf Char.IsDigit(e.KeyChar) Then
                If LoanFeeTextBox.Text.IndexOf(".") <> -1 Then
                    If LoanFeeTextBox.Text.Length >= LoanFeeTextBox.Text.IndexOf(".") + 3 Then
                        e.Handled = True
                    End If
                End If
            End If

        Else
            e.Handled = True
        End If
    End Sub

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

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