1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Windows.Forms;
|
---|
5 |
|
---|
6 | namespace Common
|
---|
7 | {
|
---|
8 | public static class PromptText
|
---|
9 | {
|
---|
10 | public static bool ShowDialog(string text, string caption, string initialValue, out string result)
|
---|
11 | {
|
---|
12 | Form prompt = new Form()
|
---|
13 | {
|
---|
14 | Width = 500,
|
---|
15 | Height = 150,
|
---|
16 | FormBorderStyle = FormBorderStyle.FixedDialog,
|
---|
17 | Text = caption,
|
---|
18 | StartPosition = FormStartPosition.CenterScreen,
|
---|
19 | };
|
---|
20 | Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text };
|
---|
21 | TextBox textBox = new TextBox() { Left = 50, Top = 44, Width = 400, Text = initialValue };
|
---|
22 | textBox.SelectAll();
|
---|
23 | Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
|
---|
24 | confirmation.Click += (sender, e) => { prompt.Close(); };
|
---|
25 | prompt.Controls.Add(textBox);
|
---|
26 | prompt.Controls.Add(confirmation);
|
---|
27 | prompt.Controls.Add(textLabel);
|
---|
28 | prompt.AcceptButton = confirmation;
|
---|
29 | prompt.Load += delegate
|
---|
30 | {
|
---|
31 | Theme.UseTheme(prompt);
|
---|
32 | DpiScaling.Apply(prompt);
|
---|
33 | new FormDimensions().Load(prompt, prompt.Owner);
|
---|
34 | };
|
---|
35 | prompt.KeyPress += (sender, e) =>
|
---|
36 | {
|
---|
37 | if (e.KeyChar == 27) prompt.Close();
|
---|
38 | };
|
---|
39 |
|
---|
40 | DialogResult dialogResult = prompt.ShowDialog();
|
---|
41 | result = (dialogResult == DialogResult.OK) ? textBox.Text : "";
|
---|
42 | return dialogResult == DialogResult.OK;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public static class PromptNumber
|
---|
47 | {
|
---|
48 | public static bool ShowDialog(string text, string caption, int initialValue, out int result)
|
---|
49 | {
|
---|
50 | Form prompt = new Form()
|
---|
51 | {
|
---|
52 | Width = 500,
|
---|
53 | Height = 150,
|
---|
54 | FormBorderStyle = FormBorderStyle.FixedDialog,
|
---|
55 | Text = caption,
|
---|
56 | StartPosition = FormStartPosition.CenterScreen
|
---|
57 | };
|
---|
58 | Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text };
|
---|
59 | NumericUpDown numericUpDown = new NumericUpDown()
|
---|
60 | {
|
---|
61 | Left = 50,
|
---|
62 | Top = 44,
|
---|
63 | Width = 80,
|
---|
64 | Maximum = 100000,
|
---|
65 | Minimum = 0,
|
---|
66 | Value = Math.Min(Math.Max(initialValue, 0), 100000)
|
---|
67 | };
|
---|
68 | numericUpDown.Select(0, numericUpDown.Text.Length);
|
---|
69 | Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
|
---|
70 | confirmation.Click += (sender, e) => { prompt.Close(); };
|
---|
71 | prompt.Controls.Add(numericUpDown);
|
---|
72 | prompt.Controls.Add(confirmation);
|
---|
73 | prompt.Controls.Add(textLabel);
|
---|
74 | prompt.AcceptButton = confirmation;
|
---|
75 | prompt.Load += delegate
|
---|
76 | {
|
---|
77 | Theme.UseTheme(prompt);
|
---|
78 | DpiScaling.Apply(prompt);
|
---|
79 | };
|
---|
80 | numericUpDown.KeyPress += (sender, e) =>
|
---|
81 | {
|
---|
82 | if (e.KeyChar == 27) prompt.Close();
|
---|
83 | };
|
---|
84 |
|
---|
85 | DialogResult dialogResult = prompt.ShowDialog();
|
---|
86 | result = (dialogResult == DialogResult.OK) ? (int)numericUpDown.Value : -1;
|
---|
87 | return dialogResult == DialogResult.OK;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public static class PromptComboBox
|
---|
92 | {
|
---|
93 | public static bool ShowDialog(string text, string caption, object[] states, object initialValue, out object result)
|
---|
94 | {
|
---|
95 | Form prompt = new Form()
|
---|
96 | {
|
---|
97 | Width = 500,
|
---|
98 | Height = 150,
|
---|
99 | FormBorderStyle = FormBorderStyle.FixedDialog,
|
---|
100 | Text = caption,
|
---|
101 | StartPosition = FormStartPosition.CenterScreen
|
---|
102 | };
|
---|
103 | Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text };
|
---|
104 | ComboBox comboBox = new ComboBox() { Left = 50, Top = 44, Width = 300, DropDownStyle = ComboBoxStyle.DropDownList };
|
---|
105 | comboBox.Items.AddRange(states);
|
---|
106 | int itemIndex = comboBox.Items.IndexOf(initialValue);
|
---|
107 | if (itemIndex >= 0)
|
---|
108 | {
|
---|
109 | comboBox.SelectedIndex = itemIndex;
|
---|
110 | }
|
---|
111 | else if (comboBox.Items.Count > 0)
|
---|
112 | {
|
---|
113 | comboBox.SelectedIndex = 0;
|
---|
114 | }
|
---|
115 | Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
|
---|
116 | confirmation.Click += (sender, e) => { prompt.Close(); };
|
---|
117 | prompt.Controls.Add(comboBox);
|
---|
118 | prompt.Controls.Add(confirmation);
|
---|
119 | prompt.Controls.Add(textLabel);
|
---|
120 | prompt.AcceptButton = confirmation;
|
---|
121 | prompt.Load += delegate
|
---|
122 | {
|
---|
123 | Theme.UseTheme(prompt);
|
---|
124 | DpiScaling.Apply(prompt);
|
---|
125 | };
|
---|
126 | comboBox.KeyPress += (sender, e) =>
|
---|
127 | {
|
---|
128 | if (e.KeyChar == 27) prompt.Close();
|
---|
129 | };
|
---|
130 |
|
---|
131 | DialogResult dialogResult = prompt.ShowDialog();
|
---|
132 | result = (dialogResult == DialogResult.OK) ? comboBox.SelectedItem : null;
|
---|
133 | return dialogResult == DialogResult.OK;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public static class PromptListBox
|
---|
138 | {
|
---|
139 | public static bool ShowDialog(string text, string caption, object[] states, object initialValue, out object result)
|
---|
140 | {
|
---|
141 | int dataSize = 200;
|
---|
142 | Form prompt = new Form()
|
---|
143 | {
|
---|
144 | Width = 500,
|
---|
145 | Height = 150 + dataSize,
|
---|
146 | FormBorderStyle = FormBorderStyle.Sizable,
|
---|
147 | Text = caption,
|
---|
148 | StartPosition = FormStartPosition.CenterScreen
|
---|
149 | };
|
---|
150 | Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text };
|
---|
151 | Button confirmation = new Button()
|
---|
152 | {
|
---|
153 | Text = @"Ok",
|
---|
154 | Left = 350,
|
---|
155 | Width = 100,
|
---|
156 | Top = 70 + dataSize,
|
---|
157 | DialogResult = DialogResult.OK,
|
---|
158 | Anchor = AnchorStyles.Bottom | AnchorStyles.Right
|
---|
159 | };
|
---|
160 | confirmation.Click += (sender, e) => { prompt.Close(); };
|
---|
161 | ListBox listBox = new ListBox()
|
---|
162 | {
|
---|
163 | Left = 50,
|
---|
164 | Top = 44,
|
---|
165 | Width = 400,
|
---|
166 | Height = dataSize,
|
---|
167 | Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left
|
---|
168 | };
|
---|
169 | listBox.SelectedIndexChanged += (sender, e) =>
|
---|
170 | {
|
---|
171 | confirmation.Enabled = listBox.SelectedItems.Count > 0;
|
---|
172 | };
|
---|
173 | listBox.Items.AddRange(states);
|
---|
174 | confirmation.Enabled = listBox.SelectedItems.Count > 0;
|
---|
175 | listBox.DoubleClick += (sender, e) => { confirmation.PerformClick(); };
|
---|
176 | int itemIndex = -1;
|
---|
177 | if (initialValue != null) itemIndex = listBox.Items.IndexOf(initialValue);
|
---|
178 | if (itemIndex >= 0)
|
---|
179 | {
|
---|
180 | listBox.SelectedIndex = itemIndex;
|
---|
181 | }
|
---|
182 | else if (listBox.Items.Count > 0)
|
---|
183 | {
|
---|
184 | listBox.SelectedIndex = 0;
|
---|
185 | }
|
---|
186 |
|
---|
187 | prompt.Controls.Add(listBox);
|
---|
188 | prompt.Controls.Add(confirmation);
|
---|
189 | prompt.Controls.Add(textLabel);
|
---|
190 | prompt.AcceptButton = confirmation;
|
---|
191 | prompt.Load += delegate
|
---|
192 | {
|
---|
193 | Theme.UseTheme(prompt);
|
---|
194 | DpiScaling.Apply(prompt);
|
---|
195 | };
|
---|
196 | listBox.KeyPress += (sender, e) =>
|
---|
197 | {
|
---|
198 | if (e.KeyChar == 27) prompt.Close();
|
---|
199 | };
|
---|
200 |
|
---|
201 | DialogResult dialogResult = prompt.ShowDialog();
|
---|
202 | result = (dialogResult == DialogResult.OK) ? listBox.SelectedItem : null;
|
---|
203 | return dialogResult == DialogResult.OK;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public static class PromptRichTextBox
|
---|
208 | {
|
---|
209 | public static bool ShowDialog(string text, string caption, string initialValue, out string result, bool monospaceFont = false)
|
---|
210 | {
|
---|
211 | int dataSize = 200;
|
---|
212 | Form prompt = new Form()
|
---|
213 | {
|
---|
214 | Width = 500,
|
---|
215 | Height = 150 + dataSize,
|
---|
216 | FormBorderStyle = FormBorderStyle.FixedDialog,
|
---|
217 | Text = caption,
|
---|
218 | StartPosition = FormStartPosition.CenterScreen,
|
---|
219 | };
|
---|
220 | Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text };
|
---|
221 | RichTextBox textBox = new RichTextBox() { Left = 50, Top = 44, Width = 400, Height = dataSize, Text = initialValue };
|
---|
222 | textBox.TextChanged += delegate
|
---|
223 | {
|
---|
224 | Theme.UseTheme(prompt);
|
---|
225 | };
|
---|
226 | textBox.SelectAll();
|
---|
227 | Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = dataSize + 70, DialogResult = DialogResult.OK };
|
---|
228 | confirmation.Click += (sender, e) => { prompt.Close(); };
|
---|
229 | prompt.Controls.Add(textBox);
|
---|
230 | prompt.Controls.Add(confirmation);
|
---|
231 | prompt.Controls.Add(textLabel);
|
---|
232 | prompt.AcceptButton = confirmation;
|
---|
233 | prompt.Load += delegate
|
---|
234 | {
|
---|
235 | Theme.UseTheme(prompt);
|
---|
236 | DpiScaling.Apply(prompt);
|
---|
237 | new FormDimensions().Load(prompt, prompt.Owner);
|
---|
238 | };
|
---|
239 | if (monospaceFont)
|
---|
240 | {
|
---|
241 | textBox.SelectAll();
|
---|
242 | textBox.SelectionFont = new Font(FontFamily.GenericMonospace, prompt.Font.Size);
|
---|
243 | textBox.SelectionLength = 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | prompt.KeyPress += (sender, e) =>
|
---|
247 | {
|
---|
248 | if (e.KeyChar == 27) prompt.Close();
|
---|
249 | };
|
---|
250 |
|
---|
251 | DialogResult dialogResult = prompt.ShowDialog();
|
---|
252 | result = (dialogResult == DialogResult.OK) ? textBox.Text : "";
|
---|
253 | return dialogResult == DialogResult.OK;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | public static class PromptOpenFile
|
---|
258 | {
|
---|
259 | public static bool ShowDialog(string fileName, string fileType, string fileExtension, out string result)
|
---|
260 | {
|
---|
261 | result = fileName;
|
---|
262 | OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = fileType + @"|*." +
|
---|
263 | fileExtension + @"|Any file|*.*", DefaultExt = fileExtension };
|
---|
264 | if (!string.IsNullOrEmpty(fileName))
|
---|
265 | {
|
---|
266 | openFileDialog.FileName = Path.GetFileName(fileName);
|
---|
267 | openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
|
---|
268 | }
|
---|
269 |
|
---|
270 | DialogResult dialogResult = openFileDialog.ShowDialog();
|
---|
271 | if (dialogResult == DialogResult.OK)
|
---|
272 | {
|
---|
273 | result = openFileDialog.FileName;
|
---|
274 | }
|
---|
275 | return dialogResult == DialogResult.OK;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | public static class PromptSaveFile
|
---|
280 | {
|
---|
281 | public static bool ShowDialog(string fileName, string fileType, string fileExtension, out string result)
|
---|
282 | {
|
---|
283 | result = fileName;
|
---|
284 | SaveFileDialog saveFileDialog = new SaveFileDialog
|
---|
285 | {
|
---|
286 | Filter = fileType + @"|*." +
|
---|
287 | fileExtension + @"|Any file|*.*",
|
---|
288 | DefaultExt = fileExtension
|
---|
289 | };
|
---|
290 | if (!string.IsNullOrEmpty(fileName))
|
---|
291 | {
|
---|
292 | saveFileDialog.FileName = Path.GetFileName(fileName);
|
---|
293 | saveFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
|
---|
294 | }
|
---|
295 |
|
---|
296 | DialogResult dialogResult = saveFileDialog.ShowDialog();
|
---|
297 | if (dialogResult == DialogResult.OK)
|
---|
298 | {
|
---|
299 | result = saveFileDialog.FileName;
|
---|
300 | }
|
---|
301 | return dialogResult == DialogResult.OK;
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | public static class PromptOpenDir
|
---|
306 | {
|
---|
307 | public static bool ShowDialog(string dirName, out string result)
|
---|
308 | {
|
---|
309 | result = dirName;
|
---|
310 | FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog() { ShowNewFolderButton = true };
|
---|
311 | if (!string.IsNullOrEmpty(dirName))
|
---|
312 | folderBrowserDialog.SelectedPath = dirName;
|
---|
313 | DialogResult dialogResult = folderBrowserDialog.ShowDialog();
|
---|
314 | if (dialogResult == DialogResult.OK)
|
---|
315 | {
|
---|
316 | result = folderBrowserDialog.SelectedPath;
|
---|
317 | }
|
---|
318 | return dialogResult == DialogResult.OK;
|
---|
319 | }
|
---|
320 | }
|
---|
321 | } |
---|