VB 编写一个文本框中对输入的字符进行转换的程序
题目要求;将其中的大写字母转换成小写字母,而小写字母则转换为大写字母,空格不换,其他字符转为“*”号,要求每输入一个字符就进行判断和转换。我画了一个文本框,命名t1代码如...
题目要求; 将其中的大写字母转换成小写字母,而小写字母则转换为大写字母,空格不换,其他字符转为“*”号,要求每输入一个字符就进行判断和转换。
我画了一个文本框,命名t1 代码如下:
Private Sub t1_Change()
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 Sub
运行时 提示堆栈空间溢出 谁能告诉怎么改? 展开
我画了一个文本框,命名t1 代码如下:
Private Sub t1_Change()
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 Sub
运行时 提示堆栈空间溢出 谁能告诉怎么改? 展开
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
可以定义一个变量保存当前状态,当改变一次就就不再执行,直到有键盘或者鼠标输入时恢复
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询