请问VB的TEXTBOX中如何限制字符长度

来源:百度知道 编辑:UC知道 时间:2024/05/18 09:44:30
Private Sub Text7_Change(Index As Integer)
Select Case Index
Case 0
If Len(Text7(0).Text) = 4 Or Len(Text7(0).Text) = 7 Then Text7(0).Text = Text7(0).Text + "/"
Text7(0).SelStart = Len(Trim(Text7(0).Text)) '把光标移动到文本框最后一个字符后
End Select
End Sub

Private Sub Text7_KeyPress(Index As Integer, KeyAscii As Integer)
Select Case Index
Case 1 To 4
If Len(Text7(Index).Text) = 4 And KeyAscii <> 8 Then KeyAscii = 0
If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If
Case 0
If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
End If

不是吧,兄弟。怎么又上这儿问来了。我不是帮你解决了吗。哈哈,有什么事直接问我就行了。

把这个直接替代原有内容,也就是添加员工姓名那个按钮的事件:
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 4
If Len(Text7(i).Text) <> 4 Then
MsgBox "请输入四位时间!", vbOKOnly + vbCritical, "提示"
Text7(i).SetFocus
Exit Sub
End If
Next i
Call zwbtj

End Sub

把这个加到Private Sub jbtimejc()这个里面,就是最下面的那个,你看最下面有个
End If
Next j
Next i
jbtimejc1 = 1

就加在这个endif的前面就行,不要加错地方了

If Len(grid1.TextMatrix(i, j)) <> 4 Then
MsgBox "职工〔" & grid1.TextMatrix(i, 1) + "〕的加班时间输入错误,请输入四位时间!", vbOKOnly + vbCritical, "提示"
grid1.Row = i
grid1.Col = j
jbtimejc1 = 0
grid1.SetFocus
Exit Sub
End If

用不着这样的,有一个属性maxlength的

只能输入数字你可