直接复制下面的代码到窗体中测试即可Private Sub Form_Load()
Dim small As String
Dim Big As String
Dim xiaoShu As Single
Dim BaifenShu As String
small = InputBox("请输入一个字符串")
Big = UCase(small)
MsgBox "你输入的字符串转化为大写后结果为:" & vbCrLf & Big
xiaoShu = InputBox("请输入一个需要转化为百分比的小数:")
BaifenShu = CStr(xiaoShu * 100) & "%"
MsgBox "你输入的小数对应的百分数为:" & BaifenShu
End Sub
例如因为你输入大写时,触发 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)
t1.Text = Mid$(t1.Text, 1, Len(t1.Text) - 1) + LCase(a)
ElseIf Asc(a) >= 97 And Asc(a)
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
严格的说来回车和Backspace键也不应该转换,转换了2个文本就跟不上进度了,只不过只有按照楼主的要去做
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90
Text2.Text = Text2.Text & LCase(Chr(KeyAscii))
Case 97 To 122
Text2.Text = Text2.Text & UCase(Chr(KeyAscii))
Case 32
Text2.Text = Text2.Text & Chr(KeyAscii)
Case Else
Text2.Text = Text2.Text & Chr(42)
End Select
End Sub
以上就是VB中实现大写字母转换和百分比计算有哪些方法?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号