稍微软糖,也许是一个更优雅的解决方案,但是......
Dim ValidKey As Boolean = True
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) Then
ValidKey = True
Else
ValidKey = False
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If ValidKey And TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
如果您愿意,您当然可以通过仅审查数字键来扩展此功能....
编辑:您可以将此代码最小化为;
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsLetterOrDigit(e.KeyChar) And TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
删除 TextChanged Sub、多余的变量和 else 语句......
编辑2:仅限于数字输入...
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) And TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
ElseIf Char.IsLetter(e.KeyChar) Then
e.Handled = True
End If
End Sub