VB组合框 如何只输入数字?

来源:百度知道 编辑:UC知道 时间:2024/06/06 14:22:53
请问,我要做一个简单组合框,只在输入数字时把其添加进列表中,输入的是字符的话不加入并且自动清空,这个怎么做啊????
源代码如下:
a = Combo1.Text
If KeyAscii = 13 Then
Combo1.AddItem a
Combo1.Text = ""
End If
If Not IsNumeric(Combo1.Text) Then
Combo1.RemoveItem Combo1.List
End If

用isnumeric函数判断就可以了

Private Sub Text1_Change()
If IsNumeric(Text1.Text) Then '如果text1的内容是数字
List1.AddItem Text1.Text 'list1添加一个项目,内容是text1的内容
Else '否则
Text1.Text = "" 'text1的内容清空
End If
End Sub
'=================================================
'其实在List1.AddItem Text1.Text这句后面也应该加一句Text1.Text = "",不然的话..呵呵,你试试就知道了