In this article, we will create a program that can generate a random password when we pressed the button.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
2. Next, add only one Button named Button1 and labeled it as "Generate Password". Add also TextBox named TextBox1 for displaying the generated password. You must design your interface like this:
3. Declare these Global Variables:
- Dim pNum As New Random(100)
- Dim pLowerCase As New Random(500)
- Dim pUpperCase As New Random(50)
- Dim password As String
- Dim RandomSelect As New Random(50)
4. Create a Function named getPassword as String with parameters of passLength as Integer and Reset as Boolean.
- Public Function getPassword(ByVal passLength As Integer, Optional ByVal Reset As Boolean = False) As String
- Dim i As Integer
- Dim ctr(2) As Integer
- Dim charSelect(2) As String
- Dim iSel As Integer
- If Reset = True Then
- password = ""
- End If
- For i = 1 To passLength
- ctr(0) = pNum.Next(48, 57) 'Numbers 1 to 9
- ctr(1) = pLowerCase.Next(65, 90) ' Lowercase Characters
- ctr(2) = pUpperCase.Next(97, 122) ' Uppercase Characters
- 'put characters in strings
- charSelect(0) = System.Convert.ToChar(ctr(0)).ToString
- charSelect(1) = System.Convert.ToChar(ctr(1)).ToString
- charSelect(2) = System.Convert.ToChar(ctr(2)).ToString
- iSel = RandomSelect.Next(0, 3)
- 'colect all characters generated through the loop
- password &= charSelect(iSel)
- If Reset = True Then
- password.Replace(password, charSelect(iSel))
- End If
- Return password
- End Function
5. Insert this code in your Button1_Click so that the function two generate password will be used in the textbox.
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- TextBox1.Text = getPassword(8, True)
- End Sub
We used the getPassword function to display the 8 digit random characters in the textbox and when we press again the button it will reset the password and generate again as we declare that the Reset variable is set to True.
Download the source code below and try it! :)
For more inquiries just contact my number or e-mail below.
Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky
Visit my page on Facebook at: https://www.facebook.com/BermzISware
Visit my website at: www.campcodes.com
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.