-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputBox.cs
More file actions
121 lines (99 loc) · 4.05 KB
/
InputBox.cs
File metadata and controls
121 lines (99 loc) · 4.05 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Utilities
{
public class InputBox
{
#region Interface
public static string ShowDialog(string prompt, string title, string defaultValue = null, int? xPos = null, int? yPos = null)
{
InputBoxDialog form = new InputBoxDialog(prompt, title, defaultValue, xPos, yPos);
DialogResult result = form.ShowDialog();
if (result == DialogResult.Cancel)
return null;
else
return form.Value;
}
#endregion
#region Auxiliary class
private class InputBoxDialog : Form
{
public string Value { get { return _txtInput.Text; } }
private Label _lblPrompt;
private TextBox _txtInput;
private Button _btnOk;
private Button _btnCancel;
#region Constructor
public InputBoxDialog(string prompt, string title, string defaultValue = null, int? xPos = null, int? yPos = null)
{
if (xPos == null && yPos == null)
{
StartPosition = FormStartPosition.CenterParent;
}
else
{
StartPosition = FormStartPosition.Manual;
if (xPos == null) xPos = (Screen.PrimaryScreen.WorkingArea.Width - Width) >> 1;
if (yPos == null) yPos = (Screen.PrimaryScreen.WorkingArea.Height - Height) >> 1;
Location = new Point(xPos.Value, yPos.Value);
}
InitializeComponent();
if (title == null) title = Application.ProductName;
Text = title;
_lblPrompt.Text = prompt;
Graphics graphics = CreateGraphics();
_lblPrompt.Size = graphics.MeasureString(prompt, _lblPrompt.Font).ToSize();
int promptWidth = _lblPrompt.Size.Width;
int promptHeight = _lblPrompt.Size.Height;
_txtInput.Location = new Point(8, 30 + promptHeight);
int inputWidth = promptWidth < 206 ? 206 : promptWidth;
_txtInput.Size = new Size(inputWidth, 21);
_txtInput.Text = defaultValue;
_txtInput.SelectAll();
_txtInput.Focus();
Height = 125 + promptHeight;
Width = inputWidth + 23;
_btnOk.Location = new Point(8, 60 + promptHeight);
_btnOk.Size = new Size(100, 26);
_btnCancel.Location = new Point(114, 60 + promptHeight);
_btnCancel.Size = new Size(100, 26);
return;
}
#endregion
#region Methods
protected void InitializeComponent()
{
_lblPrompt = new Label();
_lblPrompt.Location = new Point(12, 9);
_lblPrompt.TabIndex = 0;
_lblPrompt.BackColor = Color.Transparent;
_txtInput = new TextBox();
_txtInput.Size = new Size(156, 20);
_txtInput.TabIndex = 1;
_btnOk = new Button();
_btnOk.TabIndex = 2;
_btnOk.Size = new Size(75, 26);
_btnOk.Text = "&OK";
_btnOk.DialogResult = DialogResult.OK;
_btnCancel = new Button();
_btnCancel.TabIndex = 3;
_btnCancel.Size = new Size(75, 26);
_btnCancel.Text = "&Cancel";
_btnCancel.DialogResult = DialogResult.Cancel;
AcceptButton = _btnOk;
CancelButton = _btnCancel;
Controls.Add(_lblPrompt);
Controls.Add(_txtInput);
Controls.Add(_btnOk);
Controls.Add(_btnCancel);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
return;
}
#endregion
}
#endregion
}
}