使用“。”对字母和字母数字文本框进行文本框验证

时间:2012-02-26 13:08:32

标签: vb.net textbox validation

这就是我正在使用但我想要包含“。” dot也作为文本框中的输入

If Char.IsLetter(e.KeyChar) = False Then
    If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(Keys.Space)) Then
        e.Handled = False
    Else
        e.Handled = True
    End If
End If

If Char.IsLetterOrDigit(e.KeyChar) = False Then
    If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(Keys.Space)) Then
        e.Handled = False
    Else
        e.Handled = True
    End If
End If

4 个答案:

答案 0 :(得分:2)

要在有效字符列表中包含句点,请使用OrElse字词来测试多个条件。请注意,"."String,与"."c不同,为Char

If Not (Char.IsLetterOrDigit(e.KeyChar) OrElse e.KeyChar = "."c) Then 
    If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(Keys.Space)) Then 
        e.Handled = False 
    Else 
        e.Handled = True 
    End If 
End If 

答案 1 :(得分:0)

您可以使用我的控件:

''' <summary>
''' By Amen Ayach
''' Use RoundNumber property to set how many decimal after "."
''' example: RoundNumber = 3 so if you write 654.4444 so onlostFocus you'll see 654.444
''' </summary>
''' <remarks></remarks>
Public Class TBRound
    Inherits TextBox

    Dim Enterly As Boolean = True

    Private _RoundNumber As Integer = 0
    Public Property RoundNumber() As Integer
        Get
            Return _RoundNumber
        End Get
        Set(ByVal value As Integer)
            _RoundNumber = value
        End Set
    End Property

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    Public Function format_Number(ByVal nb As String, ByVal isDivided As Boolean, ByVal NumberAfterComma As Integer) As String
        Dim str As String = ""
        Try
            Dim fromatString As String = ""

            Dim nbs As String = "."
            For i As Integer = 0 To NumberAfterComma - 1
                nbs += "0"
            Next

            If isDivided Then
                str = "#,###"
            Else
                str = "#"
            End If

            str += nbs
            str = Format(Val(Decimal.Parse(nb.ToString())), str)
        Catch
        End Try
        Return str
    End Function

    Private Sub TBRound_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Dim allow As String = "0123456789." + ChrW(Keys.Back) + ChrW(Keys.Delete)
        If Not allow.Contains(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub TBRound_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus, Me.Validated
        Try
            If Not Decimal.TryParse(MyBase.Text, New Decimal) Then
                MyBase.Text = "0"
            Else
                ValDateMe()
            End If
        Catch
        End Try
    End Sub

    Private Sub ValDateMe()
        Try
            Dim value = MyBase.Text
            If Decimal.TryParse(MyBase.Text, New Decimal) Then
                If MyBase.Text <> format_Number(MyBase.Text, False, RoundNumber) Then
                    MyBase.Text = format_Number(MyBase.Text, False, RoundNumber)
                End If
            Else
                Enterly = False
                MyBase.Text = "0"
                Enterly = True
            End If
        Catch
        End Try
    End Sub
End Class

答案 2 :(得分:0)

您可以使用ASCII代码

If Asc(C$(F1)) > 122 Or Asc(C$(F1)) < 97 And Asc(C$(F1)) > 90 Or Asc(C$(F1)) < 65 And Asc(C$(F1)) > 57 Or Asc(C$(F1)) < 48 Then     
   Label9.Text = C$(F1) + " is an invalid Charater!"
End If

对于单个角色,您可以Or variable ="."。当你想要一个范围时,ASCII可以工作。 代码片段来自数字基础转换程序,检查数字中的无效包机,将任意基数从2转换为36.而且,通过微调可以在包括Applesoft在内的任何形式的BASIC中工作(如果,并且全部在一行,label9.text成为文本变量),也可以在VB中完成。

答案 3 :(得分:0)

您可以使用以下功能:

    Private Function StripInput(sender As String) As String
    If sender <> String.Empty Then
        Dim lastChr As String = sender(sender.Length - 1)
        Dim stripList As String = "`¬!""£$%^&*()_+-=}{[]}~#@'':?/>.<,|\;"

        If stripList.Contains(lastChr) Then
            Return sender.Substring(0, sender.Length - 1)
        Else
            Return sender
        End If
    Else
        Return sender
    End If
End Function

并从文本框的TextChanged处理程序中调用它:

    sender.text = StripInput(sender.text)
    sender.SelectionStart = sender.Text.Length + 1

如果您想要包含任何字符,只需将其从条带列表中删除即可。