• We just launched and are currently in beta. Join us as we build and grow the community.

C# - Simple Random Password Generator

KennethTejadaKGJL

Meta Breaker
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
102
Likes
22
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Simple Random Password Generator using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. It has a designed for improving productivity in the development of Web applications. It has a friendly environment for all new developers. So let's do the coding.

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want.

Here's the link for the Visual Studio https://www.visualstudio.com/
Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.
  1. namespace

    Simple_Random_Password_Generator
  2. {
  3. partial

    class

    Form1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private

    System.ComponentModel

    .

    IContainer

    components =

    null

    ;

  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected

    override

    void

    Dispose(

    bool

    disposing)
  14. {
  15. if

    (

    disposing &&

    (

    components !=

    null

    )

    )
  16. {
  17. components.

    Dispose

    (

    )

    ;
  18. }
  19. base

    .

    Dispose

    (

    disposing)

    ;
  20. }

  21. #region Windows Form Designer generated code

  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private

    void

    InitializeComponent(

    )
  27. {
  28. this

    .

    btn_generate

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Button

    (

    )

    ;
  29. this

    .

    lbl_password

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  30. this

    .

    SuspendLayout

    (

    )

    ;
  31. //
  32. // btn_generate
  33. //
  34. this

    .

    btn_generate

    .

    BackColor

    =

    System.Drawing

    .

    Color

    .

    White

    ;
  35. this

    .

    btn_generate

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 11F, System.Drawing

    .

    FontStyle

    .

    Italic

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  36. this

    .

    btn_generate

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    146

    , 261

    )

    ;
  37. this

    .

    btn_generate

    .

    Name

    =

    "btn_generate"

    ;
  38. this

    .

    btn_generate

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    225

    , 45

    )

    ;
  39. this

    .

    btn_generate

    .

    TabIndex

    =

    0

    ;
  40. this

    .

    btn_generate

    .

    Text

    =

    "Generate"

    ;
  41. this

    .

    btn_generate

    .

    UseVisualStyleBackColor

    =

    false

    ;
  42. this

    .

    btn_generate

    .

    Click

    +=

    new

    System

    .

    EventHandler

    (

    this

    .

    Generate

    )

    ;
  43. //
  44. // lbl_password
  45. //
  46. this

    .

    lbl_password

    .

    AutoSize

    =

    true

    ;
  47. this

    .

    lbl_password

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 20F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  48. this

    .

    lbl_password

    .

    ForeColor

    =

    System.Drawing

    .

    Color

    .

    Lime

    ;
  49. this

    .

    lbl_password

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    126

    , 94

    )

    ;
  50. this

    .

    lbl_password

    .

    Name

    =

    "lbl_password"

    ;
  51. this

    .

    lbl_password

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    254

    , 45

    )

    ;
  52. this

    .

    lbl_password

    .

    TabIndex

    =

    1

    ;
  53. this

    .

    lbl_password

    .

    Text

    =

    "PASSWORD"

    ;
  54. this

    .

    lbl_password

    .

    TextAlign

    =

    System.Drawing

    .

    ContentAlignment

    .

    MiddleCenter

    ;
  55. //
  56. // Form1
  57. //
  58. this

    .

    AutoScaleDimensions

    =

    new

    System.Drawing

    .

    SizeF

    (

    9F, 20F)

    ;
  59. this

    .

    AutoScaleMode

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoScaleMode

    .

    Font

    ;
  60. this

    .

    BackColor

    =

    System.Drawing

    .

    SystemColors

    .

    ActiveCaptionText

    ;
  61. this

    .

    ClientSize

    =

    new

    System.Drawing

    .

    Size

    (

    539

    , 358

    )

    ;
  62. this

    .

    Controls

    .

    Add

    (

    this

    .

    lbl_password

    )

    ;
  63. this

    .

    Controls

    .

    Add

    (

    this

    .

    btn_generate

    )

    ;
  64. this

    .

    Name

    =

    "Form1"

    ;
  65. this

    .

    ShowIcon

    =

    false

    ;
  66. this

    .

    ShowInTaskbar

    =

    false

    ;
  67. this

    .

    StartPosition

    =

    System

    .

    Windows

    .

    Forms

    .

    FormStartPosition

    .

    CenterScreen

    ;
  68. this

    .

    Text

    =

    "Simple Password Generator"

    ;
  69. this

    .

    ResumeLayout

    (

    false

    )

    ;
  70. this

    .

    PerformLayout

    (

    )

    ;

  71. }

  72. #endregion

  73. private

    System

    .

    Windows

    .

    Forms

    .

    Button

    btn_generate;
  74. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    lbl_password;
  75. }
  76. }

or also you create the layout by dragging the designating tools to the forms.

Creating the Script

This code contains the function of the application. This will code will try to trigger the event that will generate a random character when the button is clicked. To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.
  1. private

    void

    Generate(

    object

    sender, EventArgs e)
  2. {
  3. string

    characters =

    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

    ;

  4. StringBuilder stringBuilder =

    new

    StringBuilder(

    )

    ;
  5. Random random =

    new

    Random(

    )

    ;

  6. int

    passwordLength =

    random.

    Next

    (

    8

    , 13

    )

    ;

  7. while

    (

    passwordLength--

    >

    0

    )

    {
  8. stringBuilder.

    Append

    (

    characters[

    random.

    Next

    (

    characters.

    Length

    )

    ]

    )

    ;
  9. }

  10. lbl_password.

    Text

    =

    stringBuilder.

    ToString

    (

    )

    ;

  11. }

Try to run the application and see if it works.

There you go we successfully create a Simple Random Number Generator using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


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

452,292

323,348

323,357

Top