2011-12-21 110 views
1

短跑基本上我试图复制在Windows 7(在窗口)激活密钥文本框的形式。它将自动大写字母的形式,删除或拒绝除自动输入的每5个字符以外的所有非字母数字字符。VB.NET:操作文本框输入:每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 

回答

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 
+0

感谢您与它保持联系!我相信这将符合我的需求,我只需要添加一些东西,以便某些人可以右键单击并粘贴到字段中,而不是手动输入密钥。 – Zachet 2011-12-23 22:09:58

+0

请帮我解决问题,我知道解决方案远非完美,因为您无法删除,但我相信您可以添加另一个案例来轻松处理此问题。这个贴的问题将需要另一种方法,我会想,祝你好运 – Standage 2011-12-23 22:31:20

+0

我最终添加了退格功能并禁用粘贴的上下文菜单。由于发送密钥方法阻止,CTRL + V已被禁用。在将来,如果可能的话,我宁愿编辑粘贴而不是否认它,但这符合需要。 – Zachet 2012-01-06 18:29:48

3

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

+0

我喜欢这一点,唯一的问题是,当没有文字时,它会留下永久的 - 和_或空格,遗失我想要的确切效果。更糟糕的是我可以用它作为备份。 – Zachet 2011-12-21 17:46:19

0

我会建议而不是在TextBoxTextChangedEvent上处理这个问题,以便在您支持文本框的属性的Set方法中处理此问题。

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 
+0

我没有意识到插入问题,所以我很高兴你指出了这一点。我没有注意到的主要原因是因为我永远无法获得那么多。当我到达第5个字符时,按原样运行此代码会创建一个无限循环。 RegEx删除特殊字符和添加破折号的问题是它创建了一个无限循环,我需要绕过这个无限循环。整个{5}和搞砸了^在我试图解决这个问题的过程中,我忘记把它改回来。 – Zachet 2011-12-21 17:45:16

+0

我在Visual Studio上面运行了我的代码(不是使用文本框),而是传入了一些非字母数字字符的字符串,我的代码正常运行并删除所有字符,然后循环并放置 - 每5个字符。在任何时候我都没有一个无限循环? – Standage 2011-12-21 17:57:51

+0

问题在于它在'textchanged'下运行,所以无论何时更改文本框,包括删除特殊字符或添加破折号都会更改文本并重新运行。 – Zachet 2011-12-21 18:06:04