这是我的第一个教程,所以如果有任何错误,请原谅我=)。
这将为您展示一种非常简单的方法,可从外部文本文件中读取登录信息。
您需要什么:
1.在项目中添加一个新的“登录对话框”表单。
2.完全清除其中的代码,但保留接口。
3.创建一个文本文件,其中一行一行地列出您的用户名和密码。
您的文本文件应如下所示(名称前没有数字):
Username1
Password1
Username2
Password2
Username3
Password3
等等...
现在,将此代码粘贴到您的“登录”表单中。
Dim nFileNum As Integer, UInfo(999, 2) As String, sNextLine As String, lLineCount As Long, NumLine As Integer, LCount As Integer, UnPw As Integer, UnCheck As Boolean, PwCheck As Boolean, UserNum As Integer
Private Sub Form_Load()
UnCheck = False
PwCheck = False
nFileNum = FreeFile
Open "C:\Documents and Settings\Matt\My Documents\testing.txt" For Input As nFileNum
lLineCount = 1
NumLine = 0
LCount = 1
UnPw = 0
Do While Not EOF(nFileNum)
Line Input #nFileNum, sText
LCount = LCount + 1
If LCount Mod 2 <> 0 Then UnPw = 2
If LCount Mod 2 = 0 Then
NumLine = NumLine + 1
UnPw = 1
End If
UInfo(NumLine, UnPw) = sText
Loop
End Sub
Private Sub cmdCancel_Click()
Me.Hide
End
End Sub
Private Sub cmdOK_Click()
'check for correct password
For x = 1 To LCount
If txtUserName = UInfo(x, 1) Then
UnCheck = True
UserNum = x
End If
Next x
If txtPassword = UInfo(UserNum, 2) Then PwCheck = True
If txtUserName = "" Then
MsgBox "Invalid Username or Password, try again!", , "Login"
txtPassword = ""
txtPassword.SetFocus
SendKeys "{Home}+{End}"
ElseIf txtPassword = "" Then
MsgBox "Invalid Username or Password, try again!", , "Login"
txtPassword = ""
txtPassword.SetFocus
SendKeys "{Home}+{End}"
ElseIf UnCheck And PwCheck = True Then
MAINFORM.Visible = True
Me.Enabled = False
Me.Hide
Else
MsgBox "Invalid Username or Password, try again!", , "Login"
txtPassword = ""
txtPassword.SetFocus
SendKeys "{Home}+{End}"
End If
End Sub
现在,您快完成了。
1.转到第9行,并将文件路径更改为文本文件所在的位置。
2.转到第55行,将“ MAINFORM”更改为要显示的表单。
现在您应该完成! 编写程序的方式,您可能最多有999套用户名和密码。
唯一的问题是,如果有人知道您的文本文件在哪里,他们将拥有您的所有密码! 幸运的是,我只是将HAPPENS用作加密程序,因此我可以对文本文件进行加密。 完成此操作后,我将重新发布其操作方法。
From: https://bytes.com/topic/visual-basic/insights/674296-simple-way-read-login-information-text-file