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 times usernames and password you entered.
You can add additional features to your application, such as a MsgBox that would show the status, that login is successful or not. So just have a look at VB application source code for the command button.
Code For Login Command Button (VB Net Source Code)
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 the cancel button
Now test your visual basic password form, that should be perfect. In the above code, you can replace “user1” and “user2” with different usernames. “Pass1” and “pass2” with different passwords. You can also add more usernames and passwords with ‘or’ command. If you didn’t understand, click here to download the source code.