We have seen a lot of VB.net tutorials in previous posts, now you can develop a good application. But what if you are looking for some security in your application. The best thing to provide security is to add a password field or add a login form at the very beginning of the application. It is easy Coding a login form in Vb.net, so let’s see how we can do that.
How to: Coding a login form in Vb.net
For a login form, we need to drag some items to the form, drag 2 text boxes, two command buttons, and two labels. Arrange them such that they appear as the login form. (See the image below). Completely design your form!
Edit the text of labels and label them as “Username” and “Password”. The command buttons should be “Login” and “Cancel”.
Now it depends on you how much users you want to allow to access your application. The list can go on but as this is a simple login form, you should not go such long with it. The code is simple, but a little tricky. What you need to do is just program the ‘if’ statement in such a way that it checks for the number of usernames and password you entered.
You can add additional features to your application, such as a MsgBox that would show the status, that a login is successful or not. So just have a look at codes for the command button:
Code For Login Command Button:
Dim user As String user = TextBox1.Text Dim pass As String pass = TextBox2.Text If user = "user1" And pass = "pass1" Or user = "user2" And pass = "pass2" Then MsgBox("Login successful") Me.Hide() Form2.Show() Else : MsgBox("Login Unsuccessful. Please try again.") End If
For cancel button:
Now test your program, that should be perfect. In the above code, you can replace “user1” and “user2” with different usernames and “pass1” and “pass2” with different passwords and you can add more usernames and passwords with ‘or’ command. If you didn’t understand, click here to download the source code.