在KeyPress期间在字符串上提取大写字母

时间:2013-01-10 16:21:22

标签: vb.net keypress

我有两个TextBox ..

我希望在KeyPress活动期间提取/复制用户输入的所有大写字母到另一个TextBox。

逻辑:

Private Sub TextBox1_KeyPress()

'If the Character is a Capital Letter Then
'   Copy and Concatenate it to the second TextBox
'End If

End Sub

3 个答案:

答案 0 :(得分:2)

你可以试试这个:

For i = 0 To TextBox1.Text.Length - 1
    Dim c As Char = TextBox1.Text.Chars(i)
    If Char.IsUpper(c) Then
        TextBox2.AppendText(c)
    End If
Next

如果你需要它作为一个功能:

Private Function ExtractUppers(ByVal txt As TextBox) As String
    Dim sExtract As String = ""
    For i = 0 To txt.Text.Length - 1
        Dim c As Char = txt.Text.Chars(i)

        If Char.IsUpper(c) Then
            sExtract = sExtract & c
        End If
    Next

    Return sExtract
End Function

在你的按钮中:

 TextBox2.Text = ExtractUppers(TextBox1)

答案 1 :(得分:2)

我的朋友解决了! :)感谢您的回复!

Private Sub TextBox1_TextChange()

      CapitalLetter = Regex.Replace(TextBox1.Text, "[^A-Z]", String.Empty)
      TextBox2.Text = CapitalLetter

End Sub

答案 2 :(得分:0)

也许你可以使用这个技巧:

If letterVar = letterVar.ToUpper() then
    TextBox2.Text &= letterVar
End if