vb textbox 文本框 输入数字及要求 符合的给高分

用vb2做两个textbox,其中1个只能输入0-9数字,另一个只能输入15-22之间的数字,又符合的给高加分!!
If Text2 < 15 Or Text2 > 22 这句有问题 错误 2 没有为类型“System.Windows.Forms.TextBox”和“Integer”定义运算符“>”。 D:\Backup\我的文档\Visual Studio 2008\Projects\WindowsApplication6\WindowsApplication6\Form1.vb 21 26 WindowsApplication6
还有Text2 = "",错误 3 类型“String”的值无法转换为“System.Windows.Forms.TextBox”。 D:\Backup\我的文档\Visual Studio 2008\Projects\WindowsApplication6\WindowsApplication6\Form1.vb 22 21 WindowsApplication6

加入以下代码就可以实现了:

Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub

Private Sub Text1_Change()
If Len(Text1) = 2 Then Text1 = Left(Text1, Len(Text1.Text) - 1)
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub Text2_Change()
If Len(Text2) = 3 Then Text2 = Left(Text2, Len(Text2.Text) - 1)
If Len(Text2) = 2 And (Val(Text2) < 15 Or Val(Text2) > 22) Then Text2 = ""
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub Text2_LostFocus()
If Len(Text2) = 1 Then Text2.SetFocus
End Sub

已经运行过。
功能:在text1中只能输入0-9之间的数字。在text2中只能输入15-22之间的数字,当然也可以不输入(保留空的状态),否则光标不能停止输入。
还有什么问题没有?
温馨提示:答案为网友推荐,仅供参考
第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

你试一下

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网