繁体   English   中英

所以我试图调用数组并通过 If function 传递登录信息,但它一直出现错误

[英]So I'm trying to call the array and pas the login information through the If function but it keeps coming up with an error

所以我要做的是搜索员工登录信息数组,检查输入到文本框中的信息是否匹配。 如果是这样,请关闭当前表格并打开名册表格。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    Dim employee(2) As Boolean
    
    employee(0) = txtUsername.Text = "Admin" And txtPassword.Text = "AdminPass"
    employee(1) = txtUsername.Text = "User" And txtPassword.Text = "UserPass"

    'If informaiton is correct, open roster and close login      
    If txtUsername.Text And txtPassword.Text = employee(0 Or 1) Then
        Roster.Show()
        Me.Close()
    Else
        'If informaiton is incorrect, message box will open
        MessageBox.Show(String.Format("Either Username or Password is incorrect."))
    End If      
End Sub
Dim employee(2) As Boolean

这声明了索引为 0、1、2 的 3 个元素的数组。然后根据 2 个文本框的输入向元素添加值。 如果我在TextBox1键入 Admin 并在TextBox2中键入 AdminPass ,则您的数组值为TrueFalseFalse 第三个元素是False ,因为它从未被赋值,并且Boolean是一个默认值为False的值类型。

您的If语句不正确。 带有 And 的If语句需要 2 个评估为Boolean的语句。 TextBox中的.TextString而不是Boolean 请记住,您的数组仅包含 3 个元素,它们是TrueFalseFalse 没有任何意义。

我猜您想将用户输入的内容与接受的用户名和密码列表进行比较。 我做了一个Class来代表这个。 我添加了一个参数化的Sub New ,以便添加新员工。 我覆盖了.ToString ,所以我们有一个简单的比较方法。

Button.Click中,您可以创建员工列表并将员工添加到列表中。 现在您可以遍历列表,并在找到匹配项时显示下一个表单。 如果循环完成但没有匹配,则会显示失败消息框。

Public Class Employee
    Public Property UserName As String
    Public Property Password As String
    Public Sub New(uName As String, pWord As String)
        UserName = uName
        Password = pWord
    End Sub
    Public Overrides Function ToString() As String
        Return $"{UserName},{Password}"
    End Function
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lst As New List(Of Employee)
    lst.Add(New Employee("Admin", "AdminPass"))
    lst.Add(New Employee("User", "UserPass"))
    For Each emp As Employee In lst
        If $"{TextBox1.Text},{TextBox2.Text}" = emp.ToString Then
            Form2.Show()
            'Roster.Show()
            Close()
            Exit Sub
        End If
    Next
    MessageBox.Show("Either Username or Password is incorrect.")
End Sub

这只是一种方法。 有很多方法可以做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM