直接复制下面的代码到窗体中测试即可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中文网其他相关文章!