Problem:
Create a program in visual basic .net that will let the user to enter a username and password then the program will check if the username and password combination matches to the criteria set by the program.
Note: the program is no need for a database.
Solution:
We will create a login form that has no database, to do that we are going to use an if else statement and operator And.
And operator is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions.
Let’s go to work
- Open you Microsoft Visual Studio 2010, 2012 or higher, or just your Microsoft Visual Basic .Net.
- Create a new project (select File and New Project).
- For visual studio user: (select Visual Basic then Windows Form Application)
- Here is the sample form layout or design. Feel free to design your form.
We need to add the following controls:
2 labels
2 textboxes
2 buttons
1 checkbox
- Double click the OK button and paste the code below.
If TextBox1.Text = "" Then
MessageBox.Show("Please enter username")
TextBox1.Focus()
Exit Sub
ElseIf TextBox2.Text = "" Then
MessageBox.Show("Please enter password")
TextBox2.Focus()
Exit Sub
End If
If TextBox1.Text = "admin" And TextBox2.Text = "admin" Then
MessageBox.Show("welcome admin")
Else
MessageBox.Show("incorrect username or password")
End If
Code Explanation:
The program will first validate the input of the user, the user must enter a username and password or else a message will appear that will notify the user that username and password field is required.
The program will then match or compare the user input to the criteria of the program. The username must be admin and password must also be admin which means that the username and password combination must be admin or else a message will prompt you that your username and password is incorrect.
- To clear the username and password field, kindly double click the Reset button and paste the code below.
TextBox1.Clear()
TextBox2.Clear()
- Additional feature of this program is to allow the user to view or to make its password visible or in simplest explanation is to view what you are typing in the password field. Kindly double click the Show password checkbox and paste the line of codes below.
If CheckBox1.Checked = True Then
TextBox2.PasswordChar = ""
Else
TextBox2.PasswordChar = "*"
End If
Code Explanation:
If the user checks the checkbox, the user can view the characters being entered in the textbox, if the value of checkbox is unchecked then the user can only see asterisk symbol (*).
- Save and Run the project