We have seen a lot of VB.net tutorials in previous posts; now you can develop a useful application. But what if you are looking for some security in your application. The best thing to provide protection is to add a password field or add a login form at the very beginning of the application. It is easy to code a login form in Vb.net, so let’s see how we can do that.
How to code a VB.NET Login Form?
For a login form, we need to drag some items to the form, drag two 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 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 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.
As you can see, it is straightforward to make a login form by coding in VB.NET. I am sure there are many ways to use VB.NET codes for the log in form. These days, there is a module available which you can drag and drop, and it will create a login form instantly. However, if you are looking for a custom design, then you need to start from basic.