简体   繁体   English

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

[英]VB.NET: Manipulating TextBox Input: Dash every 5 characters and removing special characters

Essentially I am trying to replicate the Windows 7 (In-Windows) activation key TextBox form. 基本上我正在尝试复制Windows 7(In-Windows)激活密钥TextBox表单。 The Form where it will auto capitalize letters, remove or deny all non alphanumeric characters except dashes every 5 characters that will be auto-input. 表单,它将自动大写字母,删除或拒绝所有非字母数字字符,除了破折号,每5个字符将自动输入。

I assume this can be done with a fairly complicated replacement Regular Expression but I cannot seem to create one to fit the needs. 我认为这可以通过相当复杂的替换正则表达式来完成,但我似乎无法创建一个以满足需要。

This is an Example of what I have right now, but it creates an infinite loop as it removes all characters including dashes, than adds a dash, which changes the text and removes the dash again. 这是我现在所拥有的一个示例,但它创建了一个无限循环,因为它删除了所有字符,包括破折号,而不是添加破折号,这会更改文本并再次删除破折号。

Any suggestions, solutions or ideas would be appreciated! 任何建议,解决方案或想法将不胜感激!

Code modified from original question to the solution I am using: 代码从原始问题修改为我正在使用的解决方案:

      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

Have a look at the MaskedTextBox . 看看MaskedTextBox You should be able to achieve your aims with little code and not need the regex. 你应该能够用很少的代码实现你的目标,而不需要正则表达式。

This works, it could prob do with tidying up and comments but I just wanted to see if I could get this working: 这是有效的,它可能与整理和评论有关,但我只是想看看我是否可以使这个工作:

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

我建议不要在TextBoxTextChangedEvent上执行此操作,以在要备份文本框的属性的Set方法中处理此问题。

Look like you have the ^ in the wrong place here, it needs to be inside the square brackets, also remove the {5}-, this will then remove all the non alphanumeric characters. 看起来你在这里错误的地方^,它需要在方括号内,也删除{5} - ,这将删除所有非字母数字字符。

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

This code below will only put a - at the 5th position only and you need to add it to every 5 characters, create a loop / get the length and divide by 5 and add a "-" every 5 position. 下面的代码只会放置 - 在第5位,你需要将它添加到每5个字符,创建一个循环/得到长度并除以5并在每5个位置添加一个“ - ”。

If aKeyTextField.TextLength > 5 Then

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

  End If

Something like this would work instead: 这样的东西会起作用:

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

To stop the infinite looping change the event to Validating instead: 要停止无限循环,请将事件更改为“验证”:

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

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

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