第1个回答 2010-03-09
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Text2_LostFocus()
If Text2.text < 15 Or Text2.text > 22 Then
Text2.text = ""
MsgBox "只能输入15-22之间的数字"
End If
End Sub
.NET里???在TEXT2后面加.TEXT试试
第2个回答 2010-03-09
代码不少:
Dim text1_flag As Boolean
Dim text2_flag As Boolean
Private Sub Text1_GotFocus()
text1_flag = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If text1_flag = True And KeyAscii > 47 And KeyAscii < 58 Then
Text1.Text = ""
Else
KeyAscii = 0
End If
End Sub
Private Sub Text1_LostFocus()
text1_flag = False
End Sub
Private Sub Text2_GotFocus()
text2_flag = True
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If text2_flag = True And KeyAscii > 47 And KeyAscii < 58 Then
If Text2 <> "" Then
If Val(Text2.Text & Chr(KeyAscii)) < 15 Or Val(Text2.Text & Chr(KeyAscii)) > 22 Then
KeyAscii = 0
End If
End If
Else
KeyAscii = 0
End If
End Sub
Private Sub Text2_LostFocus()
text2_flag = False
End Sub
第3个回答 2010-03-09
我的办法比楼上两位可要简单些:
Private Sub Text1_Change()
If Val(Text1.Text) < 0 Or Val(Text1.Text) > 9 Then
MsgBox "输入越界,程序输入"
Text1.Text = ""
End If
End Sub
Private Sub Text2_Change()
If (Val(Text2.Text) < 15 And Val(Text2.Text) > 9) Or Val(Text2.Text) > 22 Then
MsgBox "输入越界,程序输入"
Text2.Text = ""
End If
End Sub
Private Sub Text2_LostFocus()
If Text2.Text < 15 Then
MsgBox "输入越界,重新输入"
Text2.SetFocus
End If
End Sub
第4个回答 2010-03-10
我也刚学vb两个星期不到,这道题我花了将近一个小时呢!好累,我吃饭去了…
Private Sub txt1_Change()
txt1.MaxLength = 1
txt2.SetFocus
End Sub
Private Sub txt1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txt2_KeyPress(KeyAscii As Integer)
txt2.MaxLength = 2
If txt2 = "" Then
Select Case KeyAscii
Case 49, 50, 8
Case Else
KeyAscii = 0
End Select
End If
If txt2 = "1" Then
Select Case KeyAscii
Case 53 To 57, 8
Case Else
KeyAscii = 0
End Select
Else
Select Case KeyAscii
Case 48 To 50, 8
Case Else
KeyAscii = 0
End Select
End If
End Sub
第5个回答 2010-03-14
Private Sub Text1_Change()
If Val(Text1.Text) >= 10 Or Val(Text1.Text) < 0 Then
MsgBox "只能输入0-9数字"
text1.text=""
text1.SetFocus
End If
End Sub
Private Sub Text2_Change()
If Val(Text2.Text) >= 10 Or Val(Text2.Text) < 0 Then
MsgBox "只能输入15-22数字"
text2.text=""
text1.SetFocus
End If
End Sub
你试一下