Can someone help me with Visual Basic Chapter 4 Welcome?

ylime 21 Reputation points
2020-12-15T13:54:27.833+00:00

I have an assignment i need to do and I'm confused. It's about entering a username and password and I'm not quite sure what to do. I tried doing the code, but it doesn't work. My main question is, how do you set a username and password in code? (If that makes sense.) Any help would be greatly appreciated and I've also attached the prompt I was given.

Public Class Form1
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

    Dim userID As String = "spicyramen06"  
    Dim password As String = "96619"  
    Static attempts As Integer = 0  

    If Me.txtUserID.Text IsNot userID And Me.txtPassword.Text IsNot password Then  
        MessageBox.Show("Access Denied")  
        Me.txtUserID.Clear()  
        Me.txtPassword.Clear()  
        attempts += 1  
    End If  

    If Me.txtUserID.Text IsNot userID Then  
        MessageBox.Show("UserID Wrong")  
        Me.txtUserID.Clear()  
        attempts += 1  
    End If  

    If Me.txtPassword.Text IsNot password Then  
        MessageBox.Show("Password Wrong")  
        Me.txtPassword.Clear()  
        attempts += 1  
    End If  

    If Me.txtUserID.Text Is userID And Me.txtPassword Is password Then  
        MessageBox.Show("Access Granted, Welcome!")  
    End If  

    If attempts > 3 Then  
        Application.Exit()  
    End If  

End Sub  

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click  

    Application.Exit()  

End Sub  

End Class

48402-2020-12-15.png

Developer technologies | Visual Studio | Debugging
Developer technologies | Visual Studio | Testing
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2020-12-15T15:15:05.177+00:00

    What is it that's not working? The user ID and password are hard coded in your example here. What happens if you type them in?

     Dim userID As String = "spicyramen06"
     Dim password As String = "96619"
    
    0 comments No comments

  2. Viorel 122.6K Reputation points
    2020-12-15T15:32:43.51+00:00

    To compare strings, try using ‘<>’ instead of ‘IsNot’ and ‘=’ instead of ‘Is”:

    If Me.txtUserID.Text <> userID And Me.txtPassword.Text <> password Then
       MessageBox.Show("Access Denied")
       Me.txtUserID.Clear()
       Me.txtPassword.Clear()
       attempts += 1
    End If
    . . .
    If Me.txtUserID.Text = userID And Me.txtPassword = password Then
       MessageBox.Show("Access Granted, Welcome!")
    End If
    

    Etc.


  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2020-12-15T17:06:26.267+00:00

    Hello @ylime

    Take a look at the following (full source is here)

    48471-login.png

    Login form

    Public Class LoginForm  
        Private userID As String = "spicyramen06"  
        Private password As String = "96619"  
        Public Shared attempts As Integer = 0  
        Private Sub LoginButton_Click(sender As Object, e As EventArgs) Handles LoginButton.Click  
      
            attempts += 1  
      
            If attempts >= 4 Then  
                LoginButton.Enabled = False  
                MessageBox.Show("Attempts exceeded")  
                Exit Sub  
            End If  
            If Not String.IsNullOrWhiteSpace(UserNameTextBox.Text) AndAlso Not String.IsNullOrWhiteSpace(PasswordTextBox.Text) Then  
                If UserNameTextBox.Text = userID AndAlso PasswordTextBox.Text = password Then  
                    My.Application.UserName = UserNameTextBox.Text  
                    Dim f As New MainForm  
                    Me.Hide()  
                    f.ShowDialog()  
                Else  
                    Controls.OfType(Of TextBox).ToList().ForEach(Sub(tb) tb.Text = "")  
                    MessageBox.Show("Invalid logn")  
                End If  
            End If  
        End Sub  
      
        Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click  
            Close()  
        End Sub  
    End Class  
    

    MyApplication class

    Namespace My  
        Partial Friend Class MyApplication  
            Public Property UserName() As String  
        End Class  
    End Namespace  
    

    Main form

    Imports System.ComponentModel  
      
    Public Class MainForm  
        Private Sub MainForm_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing  
            Application.ExitThread()  
        End Sub  
      
        Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
            Text = $"Welcome [{My.Application.UserName}]"  
        End Sub  
    End Class  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.