[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Improve the password generation program in C#

[Improve the password generation program in C#]

I use the example Generate a random password in C# a lot and I decided that it needed some updates. Lately applications are requiring longer passwords and then often require that you use special characters. The original post can handle that, but it requires that you reset its checkboxes every time you use it. This version saves your settings and restores then the next time you run the program.

To make this work, you need to first create some settings. Open Visual Studio's Project menu and select Settings at the bottom. On the Settings tab, enter the names of the settings, select the setting type (most of them in this example are Booleans), and give each setting a default value as shown in the following picture.

[Improve the password generation program in C#]

Next, make the form's Load event handler load the settings as shown in the following code.

private void Form1_Load(object sender, EventArgs e) { chkAllowLowercase.Checked = Properties.Settings.Default.AllowLowercase; chkRequireLowercase.Checked = Properties.Settings.Default.RequireLowercase; chkAllowUppercase.Checked = Properties.Settings.Default.AllowUppercase; chkRequireUppercase.Checked = Properties.Settings.Default.RequireUppercase; chkAllowNumber.Checked = Properties.Settings.Default.AllowNumber; chkRequireNumber.Checked = Properties.Settings.Default.RequireNumber; chkAllowSpecial.Checked = Properties.Settings.Default.AllowSpecial; chkRequireSpecial.Checked = Properties.Settings.Default.RequireSpecial; chkAllowUnderscore.Checked = Properties.Settings.Default.AllowUnderscore; chkRequireUnderscore.Checked = Properties.Settings.Default.RequireUnderscore; chkAllowSpace.Checked = Properties.Settings.Default.AllowSpace; chkRequireSpace.Checked = Properties.Settings.Default.RequireSpace; txtOther.Text = Properties.Settings.Default.Other; chkAllowOther.Checked = Properties.Settings.Default.AllowOther; chkRequireOther.Checked = Properties.Settings.Default.RequireOther; txtMinLength.Text = Properties.Settings.Default.MinLength.ToString(); txtMaxLength.Text = Properties.Settings.Default.MaxLength.ToString(); }

This code simply initializes the form's various check boxes and other controls to hold the saved values.

Finally, use code similar to the following to save the current settings when the program ends.

private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.AllowLowercase = chkAllowLowercase.Checked; Properties.Settings.Default.RequireLowercase = chkRequireLowercase.Checked; Properties.Settings.Default.AllowUppercase = chkAllowUppercase.Checked; Properties.Settings.Default.RequireUppercase = chkRequireUppercase.Checked; Properties.Settings.Default.AllowNumber = chkAllowNumber.Checked; Properties.Settings.Default.RequireNumber = chkRequireNumber.Checked; Properties.Settings.Default.AllowSpecial = chkAllowSpecial.Checked; Properties.Settings.Default.RequireSpecial = chkRequireSpecial.Checked; Properties.Settings.Default.AllowUnderscore = chkAllowUnderscore.Checked; Properties.Settings.Default.RequireUnderscore = chkRequireUnderscore.Checked; Properties.Settings.Default.AllowSpace = chkAllowSpace.Checked; Properties.Settings.Default.RequireSpace = chkRequireSpace.Checked; Properties.Settings.Default.Other = txtOther.Text; Properties.Settings.Default.AllowOther = chkAllowOther.Checked; Properties.Settings.Default.RequireOther = chkRequireOther.Checked; int min_length, max_length; if (int.TryParse(txtMinLength.Text, out min_length)) { Properties.Settings.Default.MinLength = min_length; } if (int.TryParse(txtMaxLength.Text, out max_length)) { Properties.Settings.Default.MaxLength = max_length; } Properties.Settings.Default.Save(); }

Notice the call to save at the end If you forget to make that call, then the settings are more saved.

In general, I prefer to save changes as soon as they occur. That way if the program or computer crashes, you don't lose any changes that you've made. For this program, that seems like overkill. It would require a bunch of event handlers to detect the changes. That would clutter the code and it doesn't seem worth it for such a simple example.

I made one more change to the program. When you click the Generate button, the program executes the following code. The blue text is new.

// Generate a new password. private void btnGenerate_Click(object sender, EventArgs e) { try { txtPassword.Text = RandomPassword(); Clipboard.Clear(); Clipboard.SetText(txtPassword.Text); lblCopied.Text = "Copied..."; tmrClearCopied.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } txtPassword.SelectAll(); txtPassword.Focus(); }

The blue code copies the new password to the clipboard, displays "Copied..." in a label below the password, and enables the tmrClearCopied timer. When that time fires one second later, it blanks the label and disables the timer.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.