简体   繁体   中英

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

So what I'm trying to do is search through the array of employee login information to check if the information entered into the text box matches it. If so, close the current form and open the Roster Form.

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

This declares and array of 3 elements with indexes 0, 1, 2. Then you add values to the elements based on the input of 2 text boxes. If I type Admin in TextBox1 and AdminPass in TextBox2 your array values are True , False , False . The third element is False because it was never assigned a value and Boolean is a value type with a default value of False .

Your If statement is not correct. An If statement with an And expects 2 statements that evaluate to Boolean . The .Text in a TextBox is a String not a Boolean . Remember your array only contains 3 elements that are True , False , False . Doesn't make any sense.

I am guessing you want to compare what the user types in to a list of accepted user names and passwords. I made a Class to represent this. I added a parameterized Sub New so it would be easy to add new employees. I overrode .ToString so we would have an easy way to compare.

In the Button.Click you create a list of employees and add the employees to the list. Now you can loop through the list and when you find a match show the next form. If the loop completes without a match the failure message box is shown.

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

This is only one approach. There are many ways to accomplish this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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