• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Password Generator in VB.NET

stiflas

Network Sniffer Expert
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
73
Likes
37
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
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:

passdesign.png


3. Declare these Global Variables:
  1. Dim

    pNum As

    New

    Random(

    100

    )
  2. Dim

    pLowerCase As

    New

    Random(

    500

    )
  3. Dim

    pUpperCase As

    New

    Random(

    50

    )
  4. Dim

    password As

    String
  5. Dim

    RandomSelect As

    New

    Random(

    50

    )

Variable pNum is used for number/numeric content of the password.
Variable pLowerCase is used for LowerCase character content of the password.
Variable pUpperCaseis used for UpperCase character content of the password.
Variable passwordis used for resetting the new password.
Variable RandomSelecis used for random selection of the password.

4. Create a Function named getPassword as String with parameters of passLength as Integer and Reset as Boolean.
  1. Public

    Function

    getPassword(

    ByVal

    passLength As

    Integer

    , Optional

    ByVal

    Reset As

    Boolean

    =

    False

    )

    As

    String
  2. Dim

    i As

    Integer
  3. Dim

    ctr(

    2

    )

    As

    Integer
  4. Dim

    charSelect(

    2

    )

    As

    String
  5. Dim

    iSel As

    Integer

clear old passwords:
  1. If

    Reset =

    True

    Then
  2. password =

    ""
  3. End

    If


create random numbers that will represent each upercase,lowercase,numbers:
  1. For

    i =

    1

    To

    passLength

  2. ctr(

    0

    )

    =

    pNum.

    Next

    (

    48

    , 57

    )

    'Numbers 1 to 9
  3. ctr(

    1

    )

    =

    pLowerCase.

    Next

    (

    65

    , 90

    )

    ' Lowercase Characters
  4. ctr(

    2

    )

    =

    pUpperCase.

    Next

    (

    97

    , 122

    )

    ' Uppercase Characters
  5. 'put characters in strings
  6. charSelect(

    0

    )

    =

    System.

    Convert

    .

    ToChar

    (

    ctr(

    0

    )

    )

    .

    ToString
  7. charSelect(

    1

    )

    =

    System.

    Convert

    .

    ToChar

    (

    ctr(

    1

    )

    )

    .

    ToString
  8. charSelect(

    2

    )

    =

    System.

    Convert

    .

    ToChar

    (

    ctr(

    2

    )

    )

    .

    ToString

pick one of the three above for a character At Random:
  1. iSel =

    RandomSelect.

    Next

    (

    0

    , 3

    )
  2. 'colect all characters generated through the loop
  3. password &=

    charSelect(

    iSel)


reset with new password:
  1. If

    Reset =

    True

    Then
  2. password.

    Replace

    (

    password, charSelect(

    iSel)

    )
  3. End

    If

  4. Next
  5. Return

    password

  6. End

    Function


5. Insert this code in your Button1_Click so that the function two generate password will be used in the textbox.
  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. TextBox1.

    Text

    =

    getPassword(

    8

    , True

    )
  3. 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.

Output:

passoutput1.png

passoutput2.png


Download the source code below and try it! :)

For more inquiries just contact my number or e-mail below.

Best Regards,

Engr. Lyndon R. Bermoy

IT Instructor/System Developer/Android Developer

Mobile: 09079373999

Telephone: 826-9296

E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,752

349,762

Top