嗨伙计们来自菜鸟的另一个问题我设法用分号分隔文本框中的数字类型,但问题是当使用退格时分号不能被删除..
我的代码就是这个
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
答案 0 :(得分:1)
轻微的软糖,也许更优雅的解决方案,但......
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
答案 1 :(得分:1)
试试这个:
Dim backPressed As Boolean = False
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If backPressed Then
backPressed = False
Return
End If
If TextBox1.TextLength Mod 3 = 2 Then
SendKeys.SendWait(":")
End If
End Sub
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = ChrW(8) Then
backPressed = True
End If
End Sub