vb 文本框输入约束问题~~

来源:百度知道 编辑:UC知道 时间:2024/09/22 04:21:05
简答说 想限制文本框输入的数的位数及数字 例如我只能在文本框中输入5位数字而且首位数字必须以0开始 如 00001、02423、 怎么限制? 当输入违反约束时弹出对话框~ “说明应该输入的格式~~~” 不是再数据库里做约束~
这段代码怎么实现~ 用按钮单击判断~
我也新手~ 只能看懂部分代码~~ 加个小注释最好了 呵呵~ 谢谢回答的朋友 我先试试看

位数可以通过 Text1.MaxLength 为设置的。
如果要设置首位为必须为0。

Private Sub Text1_KeyPress(KeyAscii As Integer)

'只允许输入数字
If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And KeyAscii <> 8 Then
KeyAscii = 0
End If

'首位必须是0
If Left(Text1.Text, 1) = "" And KeyAscii <> Asc("0") Then
KeyAscii = 0
MsgBox "首位必须是0"
End If

'设置最长为5位数
If Len(Text1.Text) = 5 And KeyAscii <> 8 Then KeyAscii = 0
End Sub

文本框的长度可以用txt.text.length来限制
根据你的想法,我试试一段code(我也是新手,不行别骂我)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim input As Integer
input += CDbl(TextBox1.Text)
If TextBox1.Text.Length > 5 Then
MsgBox("The first should be 0 and you cannot enter any more.")
TextBox1.