-
Notifications
You must be signed in to change notification settings - Fork 66
/
PasswordWindow.cs
101 lines (91 loc) · 3.1 KB
/
PasswordWindow.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Security;
using System.Windows.Forms;
namespace EasyConnect
{
/// <summary>
/// Modal window that allows the user to enter a password and makes that password available to the calling window via the <see cref="Password"/>
/// property.
/// </summary>
public partial class PasswordWindow : Form
{
/// <summary>
/// Default constructor; hides the cancel button.
/// </summary>
public PasswordWindow()
{
InitializeComponent();
cancelButton.Visible = false;
}
/// <summary>
/// Flag indicating whether we should display the cancel button.
/// </summary>
public bool ShowCancelButton
{
get
{
return cancelButton.Visible;
}
set
{
// Move the OK button on top of the cancel button if we're hiding the cancel button
okButton.Left = value
? cancelButton.Left - okButton.Width - 7
: cancelButton.Left;
cancelButton.Visible = value;
}
}
/// <summary>
/// Password entered by the user.
/// </summary>
public SecureString Password
{
get
{
return passwordTextBox.SecureText;
}
}
/// <summary>
/// Handler method that's called when the form is shown; focuses on <see cref="passwordTextBox"/>.
/// </summary>
/// <param name="e">Arguments associated with this event.</param>
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
passwordTextBox.Focus();
}
/// <summary>
/// Handler method that's called when the user clicks <see cref="okButton"/>; sets <see cref="DialogResult"/> to <see cref="DialogResult.OK"/> and
/// closes the form.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="okButton"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void okButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
/// <summary>
/// Handler method that's called when the user presses a key while in <see cref="passwordTextBox"/>; if that key is <see cref="Keys.Enter"/>, we
/// simulate the clicking of <see cref="okButton"/> by calling <see cref="okButton_Click"/>.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="passwordTextBox"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void passwordTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
okButton_Click(null, null);
}
/// <summary>
/// Handler method that's called when the user clicks <see cref="cancelButton"/>; sets <see cref="DialogResult"/> to <see cref="DialogResult.Cancel"/>
/// and closes the form.
/// </summary>
/// <param name="sender">Object from which this event originated, <see cref="cancelButton"/> in this case.</param>
/// <param name="e">Arguments associated with this event.</param>
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}