VB 编写一个文本框中对输入的字符进行转换的程序

2025-03-12 03:29:37
推荐回答(1个)
回答1:

例如因为你输入大写时,触发 text_change 事件,会改为小写,而这样又会触发 change 事件,又把小写改为大写,这样就是死循环,导致溢出

可以定义一个变量保存当前状态,当改变一次就就不再执行,直到有键盘或者鼠标输入时恢复

Dim b As Boolean

Private Sub t1_Change()
If b Then
b = False
t1.SelStart = Len(t1.Text)
a = Right(t1.Text, 1)
If Asc(a) >= 65 And Asc(a) <= 90 Then
t1.Text = Mid$(t1.Text, 1, Len(t1.Text) - 1) + LCase(a)
ElseIf Asc(a) >= 97 And Asc(a) <= 122 Then
t1.Text = Mid$(t1.Text, 1, Len(t1.Text) - 1) + UCase(a)
ElseIf Asc(a) = 32 Then
t1.Text = Mid$(t1.Text, 1, Len(t1.Text) - 1) + a
Else
t1.Text = Mid$(t1.Text, 1, Len(t1.Text) - 1) + "*"
End If
End If
End Sub

Private Sub t1_KeyDown(KeyCode As Integer, Shift As Integer)
b = True
End Sub

Private Sub t1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
b = True
End Sub