-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassword Generator.cs
More file actions
79 lines (65 loc) · 2.39 KB
/
Password Generator.cs
File metadata and controls
79 lines (65 loc) · 2.39 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Text;
namespace PasswordGenerator
{
public partial class Form1 : Form
{
private static readonly Random random = new Random();
public Form1()
{
InitializeComponent();
}
private static string GenerateRandomPassword(int length)
{
const string validChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-";
StringBuilder password = new StringBuilder();
for (int i = 0; i < length; i++)
{
password.Append(validChars[random.Next(validChars.Length)]);
}
return password.ToString();
}
private void GenerateAndDisplayPassword(int length)
{
string password = GenerateRandomPassword(length);
textBox1.Text = password;
listBox1.Items.Add(password);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
GenerateAndDisplayPassword(10);
}
catch (Exception ex)
{
MessageBox.Show("Error generating password: " + ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (listBox1.SelectedItems.Count == 0)
{
MessageBox.Show("Please select a password to copy to the clipboard.", "Error");
return; // Exit the method if nothing is selected
}
StringBuilder sb = new StringBuilder();
foreach (object row in listBox1.SelectedItems)
{
sb.AppendLine(row.ToString());
}
Clipboard.SetData(DataFormats.Text, sb.ToString().TrimEnd());
MessageBox.Show("Copied to clipboard: " + sb.ToString(), "Password Generator");
}
catch (Exception ex)
{
MessageBox.Show("Error copying to clipboard: " + ex.Message);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("The random password generator application was coded by Hasan Hüseyin KARAKAYA.", "Thank you for using it :D");
}
}
}