1

我有一个文本框,我正在使用以下内容来验证用户只能添加数字,但我怎样才能让用户只写一个“。”

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

我打算使用 contains() 或其他东西,但我怎么能意识到文本框 只有一个点?或者如何让用户可以只写一个点并且只写一个?

4

2 回答 2

2

在您的第一行之前添加此行IF

If (e.KeyChar.ToString = ".") And (txtDiagnostic.Text.Contains(e.KeyChar.ToString)) Then
     e.Handled = True
     Exit Sub
End If
于 2012-10-17T16:20:18.340 回答
-1

试试这个代码对金钱有好处

    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
于 2020-11-18T23:21:42.633 回答