Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple password generator built with C# in Visual Studio, using WPF and the .Net Framework 4.5
using System;
using System.Text;
using System.Windows;
namespace Password_Generator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private static bool addUpperCase;
private static bool addNumbers;
private static bool addSymbols;
private static string validChars;
#region Conditions
static string generatePassword(int length)
{
// Check what checkboxes are ticked
if (addUpperCase && addNumbers && addSymbols)
{
validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890?!@#$%^&*";
}
else if (addUpperCase && addNumbers)
{
validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
}
else if (addUpperCase && addSymbols)
{
validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ?!@#$%^&*";
}
else if (addNumbers && addSymbols)
{
validChars = "abcdefghijklmnopqrstuvwxyz1234567890?!@#$%^&*";
}
else if (addUpperCase)
{
validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
else if (addNumbers)
{
validChars = "abcdefghijklmnopqrstuvwxyz1234567890";
}
else if (addSymbols)
{
validChars = "abcdefghijklmnopqrstuvwxyz?!@#$%^&*";
}
else
{
validChars = "abcdefghijklmnopqrstuvwxyz";
}
// Generate password
StringBuilder res = new StringBuilder();
Random rnd = new Random();
while (0 < length--)
{
res.Append(validChars[rnd.Next(validChars.Length)]);
}
return res.ToString();
}
#endregion
#region Generate
// Generate password once button is clicked
private void btnGeneratePassword_Click(object sender, RoutedEventArgs e)
{
try
{
// Password length
int length = Convert.ToInt32(comboBox.Text);
// Generate password
string pass = generatePassword(length);
// Show password in application
password.Content = pass;
}
catch (Exception)
{
MessageBox.Show("Please select a password length.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
#region Events
// Check whether the checkbox for upper case characters is selected
public void uppercaseCheckBox_Checked(object sender, RoutedEventArgs e)
{
addUpperCase = true;
}
private void uppercaseCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
addUpperCase = false;
}
// Check whether the checkbox for numbers is selected
private void numbersCheckBox_Checked(object sender, RoutedEventArgs e)
{
addNumbers = true;
}
private void numbersCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
addNumbers = false;
}
// Check whether the checkbox for symbols is selected
private void symbolsCheckBox_Checked(object sender, RoutedEventArgs e)
{
addSymbols = true;
}
private void symbolsCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
addSymbols = false;
}
#endregion
}
}
<Window x:Class="Password_Generator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Password_Generator"
mc:Ignorable="d"
Title="MainWindow" Height="475" Width="525" MinHeight="475" MinWidth="525">
<Grid ShowGridLines="False" Margin="75,25,75,50">
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
<RowDefinition Height=".8*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="1.5*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="0.35*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label x:Name="passwordGenerator" Content="Password Generator" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" Foreground="#FF2D89EF" FontFamily="Segoe UI Semibold" FontWeight="Bold"/>
<Label x:Name="length" Content="Length:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<ComboBox x:Name="comboBox" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" SelectedIndex="0" FontSize="14">
<ComboBoxItem Content="6"/>
<ComboBoxItem Content="7"/>
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="9"/>
<ComboBoxItem Content="10"/>
<ComboBoxItem Content="11"/>
<ComboBoxItem Content="12"/>
<ComboBoxItem Content="13"/>
<ComboBoxItem Content="14"/>
<ComboBoxItem Content="15"/>
</ComboBox>
<Label x:Name="uppercase" Content="Include uppercase characters:" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<Label x:Name="uppercaseEg" Content="(eg. ABCDEF)" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<CheckBox x:Name="upperchaseCheckBox" Content="" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Checked="uppercaseCheckBox_Checked" Unchecked="uppercaseCheckBox_Unchecked"/>
<Label x:Name="numbers" Content="Include numbers:" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<Label x:Name="numbersEg" Content="(eg. 123456)" Grid.Row="3" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<CheckBox x:Name="numbersCheckBox" Content="" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Checked="numbersCheckBox_Checked" Unchecked="numbersCheckBox_Unchecked"/>
<Label x:Name="symbols" Content="Include symbols:" Grid.Row="4" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<Label x:Name="symbolsEg" Content="(eg. ?!@#$%^&amp;*)" Grid.Row="4" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
<CheckBox x:Name="symbolsCheckBox" Content="" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Checked="symbolsCheckBox_Checked" Unchecked="symbolsCheckBox_Unchecked"/>
<Label x:Name="password" Content="" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,30,0,0" FontSize="14" FontWeight="Bold"/>
<Rectangle Fill="Transparent" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,30,0,0" Height="50" Stroke="#FFB2B2B2" Width="250"/>
<Button x:Name="button" Content="Generate" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,30,0,0" Width="100" Height="30" FontSize="14" Click="btnGeneratePassword_Click" BorderBrush="{x:Null}" FontFamily="Segoe UI Semibold">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FF2D89EF"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF5AAEE8"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment