C# - Simple Random Password Generator
Submitted by razormist on Saturday, March 24, 2018 - 21:07.
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.
or also you create the layout by dragging the designating tools to the forms.
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!!!
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.- namespace Simple_Random_Password_Generator
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.SuspendLayout();
- //
- // btn_generate
- //
- this.btn_generate.BackColor = System.Drawing.Color.White;
- this.btn_generate.Font = new System.Drawing.Font("Arial", 11F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_generate.Name = "btn_generate";
- this.btn_generate.TabIndex = 0;
- this.btn_generate.Text = "Generate";
- this.btn_generate.UseVisualStyleBackColor = false;
- //
- // lbl_password
- //
- this.lbl_password.AutoSize = true;
- this.lbl_password.Font = new System.Drawing.Font("Arial", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.lbl_password.ForeColor = System.Drawing.Color.Lime;
- this.lbl_password.Name = "lbl_password";
- this.lbl_password.TabIndex = 1;
- this.lbl_password.Text = "PASSWORD";
- this.lbl_password.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // Form1
- //
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
- this.Controls.Add(this.lbl_password);
- this.Controls.Add(this.btn_generate);
- this.Name = "Form1";
- this.ShowIcon = false;
- this.ShowInTaskbar = false;
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Simple Password Generator";
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Button btn_generate;
- private System.Windows.Forms.Label lbl_password;
- }
- }
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.- private void Generate(object sender, EventArgs e)
- {
- string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- int passwordLength = random.Next(8, 13);
- while (passwordLength-- > 0) {
- stringBuilder.Append(characters[random.Next(characters.Length)]);
- }
- lbl_password.Text = stringBuilder.ToString();
- }
Add new comment
- 391 views