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"
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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"
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.
Hello @ylime
Take a look at the following (full source is here)
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
Namespace My
Partial Friend Class MyApplication
Public Property UserName() As String
End Class
End Namespace
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