为何调试状态下可以,非调试状态下不行?
有个文本框,初始值为 "ABC2D"
要求不能用vbKeyBack键删除"2",调试状态下可以,非调试状态下不行.
Private Sub Form_Load()
Text1.Text = "ABC2D"
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim nPosi As Integer
Dim sCharSelect1 As String
Dim sCharSelect2 As String
Dim sCharOri As String
sCharOri = Trim(Text1.Text)
sCharSelect2 = Text1.SelText
nPosi = Text1.SelStart
If KeyCode = vbKeyBack Then
sCharSelect1 = Mid(Trim(Text1.Text), nPosi, 1)
If sCharSelect1 = "2" Or sCharSelect2 = "2" Then
Text1.Text = sCharOri
MsgBox "不能删除指定字符!", vbInformation, "提示"
Me.Refresh
Exit Sub
End If
End If
[解决办法]
测试了下,都可以。
另外建议使用 Text1_Change ,避免鼠标的修改。