初学vb,请教
我想做一个登陆的界面的有效性验证,我写了代码,但是运行有问题,请教
我代码贴出:
Dim i As Integer
Private Sub Command1_Click()
If Text1.Text = "admin" And Text2.Text = "master" Then
Form2.Show
Unload Form1
End If
If Text1.Text = "user" And Text2.Text = "work" Then
Form2.Show
Unload Form1
Form2.Cmd5.Enabled = False
Form2.Cmd6.Enabled = False
Form2.Cmd7.Enabled = False
End If
If (Text1.Text <> "" And Text2.Text = "") Then
MsgBox "口令不能为空,请输入正确的口令!"
Text2.SetFocus
End If
If (Text1.Text = "" And Text2.Text <> "") Then
MsgBox "用户名不能为空,请正确输入用户名!"
Text1.SetFocus
End If
i = i + 1
If i = 1 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "输入错误"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
If i = 2 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "输入错误"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
If i = 3 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "非法用户"
Unload Me
End If
End Sub
问题点数:0、回复次数:12Top
1 楼liangshan(三楼楼长)回复于 2003-08-01 22:25:58 得分 0
什么问题?Top
2 楼gpo2002(永吹不休)回复于 2003-08-01 22:35:43 得分 0
Shell Function
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
Syntax
Shell(pathname[,windowstyle])
The Shell function syntax has these named arguments:
Part Description
pathname Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.
windowstyle Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus.
The windowstyle named argument has these values:
Constant Value Description
vbHide 0 Window is hidden and focus is passed to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active.
Remarks
If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.
Note By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.
Top
3 楼gpo2002(永吹不休)回复于 2003-08-01 22:37:20 得分 0
不好意思哈,贴错了Top
4 楼pigpag(Pigpag - A GRE Fighter)回复于 2003-08-01 22:58:38 得分 0
同志们,问问题的时候要把问题说清楚。
最好说明问题出在那里。
这样的程序结构非常不好:修改
Dim i As Integer
Private Sub Command1_Click()
If Text1.Text = "admin" And Text2.Text = "master" Then
Form2.Show
Unload Form1
Exit Sub '<-修改
ElseIf Text1.Text = "user" And Text2.Text = "work" Then
Form2.Cmd5.Enabled = False
Form2.Cmd6.Enabled = False
Form2.Cmd7.Enabled = False
Form2.Show
Unload Form1
Exit Sub '<-修改
ElseIf Text1.Text = "" Then '<-修改
MsgBox "用户名不能为空,请正确输入用户名!"
Text1.SetFocus
ElseIf Text2.Text = "" Then
MsgBox "口令不能为空,请输入正确的口令!"
Text2.SetFocus
Else
i = i + 1
If (Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work") Then
Select Case i
Case 1, 2
MsgBox "输入错误"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
Case 3
MsgBox "非法用户"
Unload Me
End Select
End If
End If
End Sub
这段程序很混乱,特别是后面,总觉得怪怪的,太冗长。而且
If (Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work") Then
这句判断很奇怪。是什么目的呢?你再仔细看看。Top
5 楼shenen(阿华)回复于 2003-08-01 23:14:27 得分 0
Dim i As Integer
Private Sub Command1_Click()
If Text1.Text = "admin" And Text2.Text = "master" Then
Form2.Show
Unload Form1
Exit Sub
End If
If Text1.Text = "user" And Text2.Text = "work" Then
Form2.Show
Unload Form1
Form2.cmd5.Enabled = False
Form2.cmd6.Enabled = False
Form2.cmd7.Enabled = False
Exit Sub
End If
If (Text1.Text <> "" And Text2.Text = "") Then
MsgBox "¤f¥O¤£¯à¬°ªÅ¡A½Ð¿é¤J¥¿Ú̪º¤f¥O¡I"
Text2.SetFocus
End If
If (Text1.Text = "" And Text2.Text <> "") Then
MsgBox "¥Î¤á¦W¤£¯à¬°ªÅ¡A½Ð¥¿ÚÌ¿é¤J¤J¥Î¤á¦W¡I"
Text1.SetFocus
End If
i = i + 1
If i = 1 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "¿é¤J¿ù»~"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
If i = 2 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "¿é¤J¿ù»~"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
If i = 3 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "«Dªk¥Î¤á"
Unload Me
End If
End Sub
'Let's study VB together.
'When then form is unload,You can't reference it's control.Top
6 楼shenen(阿华)回复于 2003-08-01 23:20:15 得分 0
Dim i As Integer
Private Sub Command1_Click()
If Text1.Text = "admin" And Text2.Text = "master" Then
Form2.Show
Unload Form1
Exit Sub
End If
If Text1.Text = "user" And Text2.Text = "work" Then
Form2.Show
Unload Form1
Form2.cmd5.Enabled = False
Form2.cmd6.Enabled = False
Form2.cmd7.Enabled = False
Exit Sub
End If
If (Text1.Text <> "" And Text2.Text = "") Then
MsgBox "口令不能为空,请输入正确的口令!""
Text2.SetFocus
End If
If (Text1.Text = "" And Text2.Text <> "") Then
MsgBox "用户名不能为空,请正确输入用户名!""
Text1.SetFocus
End If
i = i + 1
If i = 1 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "输入错误"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
If i = 2 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "输入错误"
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End If
If i = 3 And ((Text1.Text <> "admin" And Text2.Text <> "master") Or (Text1.Text <> "user" And Text2.Text <> "work")) Then
MsgBox "非法用户"
Unload Me
End If
End Sub
'剛才貼成了亂碼,重貼!
Top
7 楼xuanzi2002(小龙)回复于 2003-08-01 23:52:43 得分 0
to pigpag(噼里啪啦)
为什么要加上end sub 这句啊
我才学的,都不知道问什么好了Top
8 楼xuanzi2002(小龙)回复于 2003-08-01 23:59:43 得分 0
to pigpag(噼里啪啦)
你写的我不能运行,提示,ELSE 没有IFTop
9 楼huangy0153(遥遥)回复于 2003-08-02 00:45:44 得分 0
写密码程序干吗要这样写?你用combo控件吧,然后用case,不是挺方便的吗,不用老是IF了Top
10 楼csdngoodnight(居然比我还快,你真行!)回复于 2003-08-02 07:36:09 得分 0
Dim i As Integer
Private Sub Command1_Click()
if i > 3 then
msgbox"已经三次错误,退出"
'这里填上退出程序的代码
exit sub
endif
If Text1.Text = "admin" And Text2.Text = "master" Then
Form2.Show
Unload Form1
elseIf Text1.Text = "user" And Text2.Text = "work" Then
Form2.Cmd5.Enabled = False
Form2.Cmd6.Enabled = False
Form2.Cmd7.Enabled = False
Form2.Show '这里注意窗口焦点
Unload Form1
elseif (Text1.Text <> "" And Text2.Text = "") Then
MsgBox "口令不能为空,请输入正确的口令!"
Text2.SetFocus
ElseIf (Text1.Text = "" And Text2.Text <> "") Then
MsgBox "用户名不能为空,请正确输入用户名!"
Text1.SetFocus
End If
i = i + 1
End Sub
Top
11 楼zmrok(朱朱)回复于 2003-08-02 12:32:56 得分 0
if trim(text1.text)="" or trim(text2.text)="" then
msgbox"用户名和密码不为空"
exit sub
end if
select case text1.text
case "admin"
if text2.text="master" then
Form2.Show
Unload Form1
Form2.Cmd5.Enabled = False
Form2.Cmd6.Enabled = False
Form2.Cmd7.Enabled = False
else
if i > 2 then
i=i+1
msgbox"密码错误"
exit sub
else
msgbox"非法用户"
exit sub
end if
end if
case "user"
if text2.text="work" then
Form2.Show
Unload Form1
Form2.Cmd5.Enabled = False
Form2.Cmd6.Enabled = False
Form2.Cmd7.Enabled = False
else
if i > 2 then
i=i+1
msgbox"密码错误"
exit sub
else
msgbox"非法用户"
exit sub
end if
end if
end select
Top
12 楼xuanzi2002(小龙)回复于 2003-08-03 15:04:05 得分 0
谢大家了Top




