VB.NET:操作TextBox输入:每5个字符删除一次并删除特殊字符

时间:2011-12-21 16:43:21

标签: regex vb.net

基本上我正在尝试复制Windows 7(In-Windows)激活密钥TextBox表单。表单,它将自动大写字母,删除或拒绝所有非字母数字字符,除了破折号,每5个字符将自动输入。

我认为这可以通过相当复杂的替换正则表达式来完成,但我似乎无法创建一个以满足需求。

这是我现在所拥有的一个示例,但是它会创建一个无限循环,因为它会删除所有字符,包括短划线,而不是添加短划线,这会更改文本并再次删除短划线。

任何建议,解决方案或想法都将不胜感激!

将代码从原始问题修改为我正在使用的解决方案:

      Private Sub aKeyTextField_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles aKeyTextField.KeyPress
    'Test only
    If Char.IsLetterOrDigit(e.KeyChar) Then
        Dim test As String = ""
        Select Case e.KeyChar
            Case "0" To "9"
                ' Do nothing
            Case "a" To "z"
                e.KeyChar = Char.ToUpper(e.KeyChar)
            Case Else
                e.Handled = True
        End Select
    ElseIf Not e.KeyChar = Convert.ToChar(8) Then

        e.KeyChar = ""
        End If


        Dim strKeyTextField As String = aKeyTextField.Text
        Dim n As Integer = 5
    Dim intlength As Integer = aKeyTextField.TextLength

    If Not e.KeyChar = Convert.ToChar(8) Then

        While intlength > 4 And intlength < 29

            If aKeyTextField.Text.Length = 5 Then
                strKeyTextField = strKeyTextField.Insert(5, "-")

            End If

            Dim singleChar As Char
            singleChar = strKeyTextField.Chars(n)

            While (n + 5) < intlength

                If singleChar = "-" Then
                    n = n + 6

                    If n = intlength Then
                        strKeyTextField = strKeyTextField.Insert(n, "-")
                    End If
                End If

            End While

            intlength = intlength - 5
        End While
        aKeyTextField.Text = strKeyTextField

        'sets focus at the end of the string
        aKeyTextField.Select(aKeyTextField.Text.Length, 0)


    Else

        Select Case aKeyTextField.Text.Length
            Case 7
                strKeyTextField = strKeyTextField.Remove(5, 1)
            Case 13
                strKeyTextField = strKeyTextField.Remove(11, 1)
            Case 19
                strKeyTextField = strKeyTextField.Remove(17, 1)
            Case 25
                strKeyTextField = strKeyTextField.Remove(23, 1)
            Case Else



        End Select

        aKeyTextField.Text = strKeyTextField
        aKeyTextField.Select(aKeyTextField.Text.Length, 0)

    End If

End Sub

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim blankContextMenu As New ContextMenu
    aKeyTextField.ContextMenu = blankContextMenu


End Sub

4 个答案:

答案 0 :(得分:3)

查看MaskedTextBox。你应该能够用很少的代码实现你的目标,而不需要正则表达式。

答案 1 :(得分:1)

这很有效,它可以用整理和评论来解决,但我只想看看能不能做到这一点:

Private Sub aKeyTextField_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles aKeyTextField.KeyPress
    'Test only
    If Char.IsLetterOrDigit(e.KeyChar) Then
        Dim test As String = ""
        Select Case e.KeyChar
            Case "0" To "9"
                ' Do nothing
            Case "a" To "z"
                e.KeyChar = Char.ToUpper(e.KeyChar)
            Case Else
                e.Handled = True
        End Select
    Else
        e.KeyChar = ""
    End If


    Dim strKeyTextField As String = aKeyTextField.Text
    Dim n As Integer = 5
    Dim intlength As Integer = aKeyTextField.TextLength
    While intlength > 4

        If aKeyTextField.Text.Length = 5 Then
            strKeyTextField = strKeyTextField.Insert(5, "-")

        End If

        Dim singleChar As Char
        singleChar = strKeyTextField.Chars(n)

        While (n + 5) < intlength

            If singleChar = "-" Then
                n = n + 6

                If n = intlength Then
                    strKeyTextField = strKeyTextField.Insert(n, "-")
                End If
            End If

        End While

        intlength = intlength - 5
    End While
    aKeyTextField.Text = strKeyTextField

    'sets focus at the end of the string
    aKeyTextField.Select(aKeyTextField.Text.Length, 0)

End Sub

答案 2 :(得分:0)

我建议您不要在TextChangedEvent的{​​{1}}上执行此操作,以便在您支持文本框的属性的TextBox方法中处理此问题。

答案 3 :(得分:0)

看起来你在这里错误的地方^,它需要在方括号内,也删除{5} - ,这将删除所有非字母数字字符。

 If m.Success Then
            aKeyTextField.Text = Regex.Replace(aKeyTextField.Text, "[^a-zA-Z0-9]+, "")"
            aKeyTextField.SelectionStart = aKeyTextField.TextLength
 End If

下面这段代码只会在第5个位置添加 - 你需要将它添加到每5个字符,创建一个循环/得到长度并除以5并在每5个位置添加一个“ - ”。

If aKeyTextField.TextLength > 5 Then

        aKeyTextField.Text = aKeyTextField.Text.Insert(5, "-")
        aKeyTextField.SelectionStart = aKeyTextField.TextLength

  End If

这样的事情会起作用:

Dim n As Integer = 5
        Dim intlength As Integer = aKeyTextField.Length
        While intlength > 0 And n < intlength
            aKeyTextField = aKeyTextField.Insert(n, "-")
            intlength = intlength - 5
            n = n + 6
        End While

要停止无限循环,请将事件更改为“验证”:

Private Sub aKeyTextField_TextValidating(ByVal sender As Object, ByVal e As System.EventArgs) Handles aKeyTextField.Validating