请教在TextBox控件里面的输入问题

dl2009 2008-11-12 11:34:19
怎样才能在TextBox里面限制只能输入0811 .就是输入的只能是两位的年份和2位的月份.

当中不能输入0000.
...全文
66 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dl2009 2008-11-12
  • 打赏
  • 举报
回复
感谢大家
Amy_Dream 2008-11-12
  • 打赏
  • 举报
回复
上一个错了

Option Explicit

Private Sub Form_Load()
Text1.MaxLength = 4
Text1 = ""
End Sub

Private Sub Text1_Change()
Dim strS As String
strS = Text1
If Len(Text1) > 2 Then
If CInt(Mid(strS, 3, Len(Text1) - 2)) > 12 Then
Text1.SelStart = 2
Text1.SelLength = 2
End If
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub

of123 2008-11-12
  • 打赏
  • 举报
回复

如果是商业软件,最好不用事后拒绝的方式。让用户输入之后再说“你错了”,会让人觉得不够友好。

如果是自用测试软件,那就无妨了,甚至可以不做检查,自己输入的时候多加小心。
Amy_Dream 2008-11-12
  • 打赏
  • 举报
回复

Option Explicit

Private Sub Form_Load()
Text1.MaxLength = 4
Text1 = ""
End Sub

Private Sub Text1_Change()
Dim strS As String
strS = Text1
If Len(Text1) > 2 Then
If CInt(Mid(strS, 3, Len(Text1) - 2)) > 30 Then
Text1.SelStart = 2
Text1.SelLength = 2
End If

Else
If CInt(Left(strS, 2)) > 12 Then
Text1.SelStart = 0
Text1.SelLength = 2
End If
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub

vbman2003 2008-11-12
  • 打赏
  • 举报
回复
还是输入完毕再判断简单,简化一下前面的代码:

Private Sub Text1_LostFocus()
If (Text1 Like "####") And (IsDate(Format("19" & Text1, "####-##"))) Then
MsgBox "合法"
Else
MsgBox "非法"
End If
End Sub
of123 2008-11-12
  • 打赏
  • 举报
回复

比较麻烦:

Private Sub Text1_KeyPress(KeyAscii As Integer)

Select Case Text1.SelStart
Case 0
Select Case KeyAscii
Case 8, 13, 48 To 57
Case Else
KeyAscii = 0
End Select
Case 1
Select Case KeyAscii
Case 8, 9, 13, 48 To 57
Case Else
KeyAscii = 0
End Select
Case 2
Select Case KeyAscii
Case 8, 9, 13, 48 To 49
Case Else
KeyAscii = 0
End Select
Case 3
Select Case KeyAscii
Case 8, 9, 13
Case 48 To 57
If (Mid(Text1, 3, 1) & Chr(KeyAscii) = "00") Or (Mid(Text1, 3, 1) & Chr(KeyAscii) > 12) Then KeyAscii = 0
Case Else
KeyAscii = 0
End Select

End Select

End Sub
vbman2003 2008-11-12
  • 打赏
  • 举报
回复
Private Sub Text1_LostFocus()
If (Text1 Like "####") And (IsDate(Format(Format("19" & Text1, "####-##"), "yyyy-mm"))) Then
MsgBox "合法"
Else
MsgBox "非法"
End If
End Sub
Forrest23 2008-11-12
  • 打赏
  • 举报
回复
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Me.TextBox1.MaxLength = 4
If Char.IsNumber(e.KeyChar) = False Then
If Char.IsControl(e.KeyChar) = True Then
e.Handled = False
Else
e.Handled = True
End If
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Me.TextBox1.Text = "00" Then
Me.TextBox1.Text = ""
End If
End Sub
jhone99 2008-11-12
  • 打赏
  • 举报
回复
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
MsgBox "?"
Text1.Text = ""
Text1.SetFocus
End If

If Len(Text1.Text) > 4 Then
MsgBox "?"
Text1.Text = Left(Text1.Text, 4)
End If

If Mid(Text1.Text, 3, 2) > 12 Then
MsgBox "?"
Text1.Text = Left(Text1.Text, 2)
End If

End Sub

SYSSZ 2008-11-12
  • 打赏
  • 举报
回复
Private Sub Text1_LostFocus()
b = Left(Text1.Text, 2)
If Len(Trim(Text1.Text)) = 4 And IsNumeric(Text1.Text) And CInt(b) <> 0 And CInt(b) < 13 Then
MsgBox "ok"
Else
MsgBox "no"
End If
End Sub

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧