How to create a password protected form in Visual Basic.Net


In this article I am going to show you how you can create a password protected form in Microsoft Visual Basic.Net.This is just like the windows serial key protection on the form. I have set the Maximum length of the Text Boxes to 5 and after that focus is automatically shifted to the Next Text Box. After hitting the OK Button, if the password is corrected form 2 will be displayed otherwise a message on a label will be displayed that the serial key is not valid.

In this article I am going to show you how you can create a password protected form in Microsoft Visual Basic.Net. First of all you need to create a form and place two labels, 5 Text Boxes and 3 Buttons on the form as shown in the image below with lebel1 having no text.
Password Protected Form in VB.Net

Now add form2 but make form1 as the startup form of your project.
Now, open the code window of form1 and add the code as shown below against each control on your form (I have changed the name of 3 Textboxes).


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.MaxLength = 5
TextBox2.MaxLength = 5
TextBox3.MaxLength = 5
TextBox4.MaxLength = 5
TextBox5.MaxLength = 5
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Focus()
End If
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.TextLength = TextBox1.MaxLength Then
TextBox3.Focus()
End If
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
If TextBox3.TextLength = TextBox1.MaxLength Then
TextBox4.Focus()
End If
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
If TextBox4.TextLength = TextBox1.MaxLength Then
TextBox5.Focus()
End If
End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
If TextBox5.TextLength = TextBox1.MaxLength Then
btnOk.Focus()

End If
End Sub
Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
End
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
btnOk.Enabled = True
Label1.Text = ""

TextBox1.Focus()
For Each Control In Controls
If TypeOf Control Is TextBox Then
Control.Text = ""
End If
Next Control
End Sub

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
If TextBox1.Text = "YVGKQ" And TextBox2.Text = "B7QYX" And TextBox3.Text = "WWPBR" And TextBox4.Text = "M7T2R" And TextBox5.Text = "PB8BM" Then
Me.Hide()
Form2.Show()
Else
Label1.Text = "Invalid Serial Key!"
btnOk.Enabled = False
btnClear.Focus()

End If
End Sub


Regards
Keep smiling


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: