-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
52 lines (44 loc) · 1.63 KB
/
Copy pathForm1.cs
File metadata and controls
52 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace PasswordGenerator
{
public partial class Form1 : Form
{
private readonly Dictionary<string, string> characterSets = new Dictionary<string, string>
{
{ "Lowercase", "abcdefghijklmnopqrstuvwxyz" },
{ "Uppercase", "ABCDEFGHIJKLMNOPQRSTUVWXYZ" },
{ "Digits", "0123456789" },
{ "Special Characters", "!@#$%^&*()_+" }
};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Build the character set based on selected items in the CheckedListBox
string characters = "";
foreach (string selectedCharacterType in checkedListBox1.CheckedItems)
{
characters += characterSets[selectedCharacterType];
}
// Set the desired length of the password
int passwordLength = int.Parse(textBox2.Text);
// Generate a random password
string password = GenerateRandomPassword(characters, passwordLength);
// Display the password in the textBox1
textBox1.Text = password;
}
private string GenerateRandomPassword(string characters, int length)
{
Random random = new Random();
return new string(Enumerable.Repeat(characters, length).Select(s => s[random.Next(s.Length)]).ToArray());
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}