纪念谷碑:VB 空字符

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 09:15:21
我在VB上创建了一个文本框和一个命令按扭。我想点命令按扭时限制文本框不能输入空、空字符、开头第一个也不能为数字。要以下划线,字母或字符开头。。我要如何做!?

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Len(Text1) > 0 Then
If KeyAscii = 13 Then
If Asc(Left(Text1, 1)) = 95 Or Asc(Left(Text1, 1)) >= 65 And Asc(Left(Text1, 1)) <= 90 _
Or Asc(Left(Text1, 1)) >= 97 And Asc(Left(Text1, 1)) <= 122 Then
Text1 = Text1
Command1.SetFocus
Else
Text1 = ""
End If
End If
End If
End Sub

Private Sub Command1_Click()
Dim s As String
s = Text1.Text
If Len(s) = 0 Then
MsgBox "不能提交空信息"
Else
If Asc(Left(s, 1)) >= 48 And Asc(Left(s, 1)) <= 57 Then
MsgBox "提交的信息不合法!" & Asc(s)
Else
MsgBox "提交的格式正确"
End If
End If
End Sub

dim Txt as string
txt=left$(text1.text,1)
select case txt
case ""
msgbox "不合法!"
case "1"
msgbox "不合法!"
case "2"
msgbox "不合法!"
case "3"
msgbox "不合法!"
.
.
.
.
.
.

命令按钮事件下对文本框的内容进行检查(用IF语句)
如果不符合条件,就弹出一个提示,然后返回。如果符合条件,就继续执行其他命令。
检查是否空:判断文本框内容的长度是否为零
检查开头第一个是否为数字:判断文本框内容第一个字符的ASCII码是否在数字的ASCII码值之间

查资料