将字符串拆分为Sub String

时间:2018-01-07 19:34:56

标签: vb.net

我正在使用Vb.net。我的想法是当我粘贴(ctrl + v)例如这个字符时:

AHAKAPATARAE

我有6个文本框。 它会自动将它们按顺序逐个粘贴到文本框中!

所以 txtBox1将包含:AH

txtBox2:AK

txtBox3:AP

txtBox4:AT

texbox5:AR

texbox6:AE

插入许可证密钥的自动化将非常容易 这样用户就不会那么努力去切割&粘贴每两位数字!

所以在文本框中自动填充的任何建议......?

感谢。

2 个答案:

答案 0 :(得分:0)

处理文本框1上的按键事件

Private Sub txtBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtBox1.KeyDown
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.V Then

    ' Get Clipboard Text
    Dim cpText as String = My.Computer.Clipboard.GetText()

    ' Assign
    txtBox1.Text = cpText.Substring(0, 2)
    txtBox2.Text = cpText.Substring(2, 2)
    txtBox3.Text = cpText.Substring(4, 2)
    txtBox4.Text = cpText.Substring(6, 2)
    txtBox5.Text = cpText.Substring(8, 2)
    txtBox6.Text = cpText.Substring(10, 2)

    'the event has been handled manually
    e.Handled = True
End If
End Sub

另一个选择是有一个蒙版文本框事件。可能会更容易

答案 1 :(得分:0)

如果用户从第一个框开始手动输入整个内容,这将允许粘贴到第一个文本框中,以及正确前进:

Private Sub txtBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBox6.TextChanged, txtBox5.TextChanged, txtBox4.TextChanged, txtBox3.TextChanged, txtBox2.TextChanged, txtBox1.TextChanged
    Dim TB As TextBox = DirectCast(sender, TextBox)
    Dim value As String = TB.Text
    If value.Length > 2 Then
        TB.Text = value.Substring(0, 2)
        Dim TBs() As TextBox = {txtBox1, txtBox2, txtBox3, txtBox4, txtBox5, txtBox6}
        Dim index As Integer = Array.IndexOf(TBs, TB)
        If index > -1 AndAlso index < (TBs.Length - 1) Then
            index = index + 1
            TBs(index).Text = value.Substring(2)
            TBs(index).Focus()
            TBs(index).SelectionStart = TBs(index).TextLength
        End If
    End If
End Sub

肯定还有很多方法可以实现......