C# - Simple Random Password Generator

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.  
  10.         /// <summary>
  11.         /// Clean up any resources being used.
  12.         /// </summary>
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14.         protected override void Dispose(bool disposing)
  15.         {
  16.             if (disposing && (components != null))
  17.             {
  18.                 components.Dispose();
  19.             }
  20.             base.Dispose(disposing);
  21.         }
  22.  
  23.         #region Windows Form Designer generated code
  24.  
  25.         /// <summary>
  26.         /// Required method for Designer support - do not modify
  27.         /// the contents of this method with the code editor.
  28.         /// </summary>
  29.         private void InitializeComponent()
  30.         {
  31.             this.btn_generate = new System.Windows.Forms.Button();
  32.             this.lbl_password = new System.Windows.Forms.Label();
  33.             this.SuspendLayout();
  34.             //
  35.             // btn_generate
  36.             //
  37.             this.btn_generate.BackColor = System.Drawing.Color.White;
  38.             this.btn_generate.Font = new System.Drawing.Font("Arial", 11F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  39.             this.btn_generate.Location = new System.Drawing.Point(146, 261);
  40.             this.btn_generate.Name = "btn_generate";
  41.             this.btn_generate.Size = new System.Drawing.Size(225, 45);
  42.             this.btn_generate.TabIndex = 0;
  43.             this.btn_generate.Text = "Generate";
  44.             this.btn_generate.UseVisualStyleBackColor = false;
  45.             this.btn_generate.Click += new System.EventHandler(this.Generate);
  46.             //
  47.             // lbl_password
  48.             //
  49.             this.lbl_password.AutoSize = true;
  50.             this.lbl_password.Font = new System.Drawing.Font("Arial", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  51.             this.lbl_password.ForeColor = System.Drawing.Color.Lime;
  52.             this.lbl_password.Location = new System.Drawing.Point(126, 94);
  53.             this.lbl_password.Name = "lbl_password";
  54.             this.lbl_password.Size = new System.Drawing.Size(254, 45);
  55.             this.lbl_password.TabIndex = 1;
  56.             this.lbl_password.Text = "PASSWORD";
  57.             this.lbl_password.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  58.             //
  59.             // Form1
  60.             //
  61.             this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  62.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  63.             this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
  64.             this.ClientSize = new System.Drawing.Size(539, 358);
  65.             this.Controls.Add(this.lbl_password);
  66.             this.Controls.Add(this.btn_generate);
  67.             this.Name = "Form1";
  68.             this.ShowIcon = false;
  69.             this.ShowInTaskbar = false;
  70.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  71.             this.Text = "Simple Password Generator";
  72.             this.ResumeLayout(false);
  73.             this.PerformLayout();
  74.  
  75.         }
  76.  
  77.         #endregion
  78.  
  79.         private System.Windows.Forms.Button btn_generate;
  80.         private System.Windows.Forms.Label lbl_password;
  81.     }
  82. }
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.  
  5.             StringBuilder stringBuilder = new StringBuilder();
  6.             Random random = new Random();
  7.  
  8.             int passwordLength = random.Next(8, 13);
  9.  
  10.             while (passwordLength-- > 0) {
  11.                 stringBuilder.Append(characters[random.Next(characters.Length)]);
  12.             }
  13.  
  14.             lbl_password.Text = stringBuilder.ToString();
  15.  
  16.         }
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!!!

Add new comment