求VB之工程代码

来源:百度知道 编辑:UC知道 时间:2024/06/07 10:43:00
要求如下:在Text1中输入密码,且为6位数,?号表示,回车结束,当输入正确时Text2显示“密码正确”,如果输入错误,则Text2显示“密码错误,重新输入”,同时消除Text1中的密码,将焦点重新定位于Text1中!!!!!!!!!!!!谢谢大侠

先将text1的password属性设为“?”;
代码如下:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Len(Text1.Text) <> 6 Then
MsgBox "请输入6位密码!"
Exit Sub
End If
If Text1.Text = "123456" Then
Text2.Text = "密码正确"
Else
Text2.Text = "密码错误"
Text1.Text = ""
Text1.SetFocus
End If
End If
End Sub

向窗口添加 Text1 和 Text2,具体代码如下:(其实 Text1.SetFocus 是不必要的,按下回车时焦点本身就在Text1)

Private Sub Form_Load()
With Text1
.MaxLength = 6
.PasswordChar = "?"
End With
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13
If Text1.Text = "123456" Then
Text2.Text = "正确"
Else
Text1.