在文本框中输入数字后自动调整格式
编号:QA000144
建立日期: 1998年11月28日 最后修改日期:2003年11月7日
所属类别:
Q
Hongtao:
怎么能在TextBox控件实现类似于dbase数值型中的小数点, 即想达到“在文本框中输入一个[200]回车后自动变成[200.00]的效果”。
A回答:
关于类似200向200.00的转换可用如下程序实现:
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Text1.Text = re(Text1.Text)
End If
End Sub
Private Function re(str As String) As String
Dim s1, s2, s3 As String, i As Integer
i = InStrB(str, ".")
If i = 0 Then
re = str + ".00"
Exit Function
End If
s1 = Right(str, 2)
s2 = Left(s1, 1)
If s2 = "." Then
re = str + "0"
Exit Function
End If
re = Text1.Text
End Function
以上代码所用到的事件、方法、函数的详细用法请参阅VB Help。用类似方法还可实现诸如123456向123,456.00的转换。
李林华的意见:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text1.Text = Format(Text1.Text, "##,###.00")
End If
End Sub
此问题由Kurt回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 标准控件, screen, button, combo, checkbox, listbox。
| |
|
|
| |
|
|