在ComboBox的KeyPress事件中加如下代码:if keyascii<asc("0") or keyascii>asc("9") then keyascii=0
在ComboBox的KeyPress事件中加如下代码:if keyascii<asc("0") or keyascii>asc("9") then keyascii=0:exit sub if len(ComboBox.text)=2 then keyascii=0
Private Sub Combo1_KeyPress(KeyAscii As Integer) Dim i As Long Dim str As String If KeyAscii >= &H30 And KeyAscii <= &H39 Then Combo1.SelText = ""
str = Combo1.Text & Chr(KeyAscii) If Len(str) > 2 Then KeyAscii = 0 End If
str = Left(str & "*", 2) For i = 0 To Combo1.ListCount - 1 If Combo1.List(i) Like str Then Exit For End If Next
If i >= Combo1.ListCount Then KeyAscii = 0 End If End If End SubPrivate Sub Form_Load() Dim i As Long For i = 0 To 23 Combo1.AddItem Format(i, "00") Next End Sub
稍微改了一点 Private Sub Combo1_KeyPress(KeyAscii As Integer) Dim i As Long Dim str As String If KeyAscii >= &H30 And KeyAscii <= &H39 Then Combo1.SelText = ""
If Combo1.SelStart > 0 Then str = Combo1.Text & Chr(KeyAscii) Else str = Chr(KeyAscii) & Combo1.Text End If
If Len(str) > 2 Then KeyAscii = 0 End If
str = Left(str & "*", 2) For i = 0 To Combo1.ListCount - 1 If Combo1.List(i) Like str Then Exit For End If Next
If i >= Combo1.ListCount Then KeyAscii = 0 End If End If End Sub
Private Sub cbo_KeyPress(KeyAscii As Integer) If KeyAscii >= 48 And KeyAscii <= 57 Then '判断输入的是否是数字 If cbo.SelLength = 0 Then '判断是否有选中的字符 If Len(cbo.Text) >= 2 Then '判断组合框中的内容是否已是两位数(如果不写这句会输入多个0) KeyAscii = 0 Else If CInt(cbo.Text & Chr(KeyAscii)) > 23 Then KeyAscii = 0 '判断输入后数字是否已大于23 End If End If ElseIf Not (KeyAscii = 8) Then '判断输入的 不是 退格键(此判断不能少) KeyAscii = 0 End If End Sub
if len(ComboBox.text)=2 then keyascii=0
Dim i As Long
Dim str As String
If KeyAscii >= &H30 And KeyAscii <= &H39 Then
Combo1.SelText = ""
str = Combo1.Text & Chr(KeyAscii)
If Len(str) > 2 Then
KeyAscii = 0
End If
str = Left(str & "*", 2)
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) Like str Then
Exit For
End If
Next
If i >= Combo1.ListCount Then
KeyAscii = 0
End If
End If
End SubPrivate Sub Form_Load()
Dim i As Long
For i = 0 To 23
Combo1.AddItem Format(i, "00")
Next
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim i As Long
Dim str As String
If KeyAscii >= &H30 And KeyAscii <= &H39 Then
Combo1.SelText = ""
If Combo1.SelStart > 0 Then
str = Combo1.Text & Chr(KeyAscii)
Else
str = Chr(KeyAscii) & Combo1.Text
End If
If Len(str) > 2 Then
KeyAscii = 0
End If
str = Left(str & "*", 2)
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) Like str Then
Exit For
End If
Next
If i >= Combo1.ListCount Then
KeyAscii = 0
End If
End If
End Sub
If KeyAscii >= 48 And KeyAscii <= 57 Then '判断输入的是否是数字
If cbo.SelLength = 0 Then '判断是否有选中的字符
If Len(cbo.Text) >= 2 Then '判断组合框中的内容是否已是两位数(如果不写这句会输入多个0)
KeyAscii = 0
Else
If CInt(cbo.Text & Chr(KeyAscii)) > 23 Then KeyAscii = 0 '判断输入后数字是否已大于23
End If
End If
ElseIf Not (KeyAscii = 8) Then '判断输入的 不是 退格键(此判断不能少)
KeyAscii = 0
End If
End Sub