source: Common/Prompt.cs

Last change on this file was 15, checked in by chronos, 13 months ago
  • Modified: Updated files.
File size: 12.9 KB
Line 
1using System;
2using System.IO;
3using System.Drawing;
4using System.Windows.Forms;
5
6namespace 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 const 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 const int dataSize = 200;
212 Form prompt = new Form()
213 {
214 Width = 500,
215 Height = 150 + dataSize,
216 FormBorderStyle = FormBorderStyle.Sizable,
217 Text = caption,
218 StartPosition = FormStartPosition.CenterScreen
219 };
220 Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text };
221 RichTextBoxEx textBox = new RichTextBoxEx()
222 {
223 Left = 50, Top = 44, Width = 400, Height = dataSize, Text = initialValue,
224 Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left
225 };
226 textBox.TextChanged += delegate
227 {
228 Theme.UseTheme(prompt);
229 };
230 textBox.SelectAll();
231 Button confirmation = new Button()
232 {
233 Text = @"Ok", Left = 350, Width = 100, Top = dataSize + 70, DialogResult = DialogResult.OK,
234 Anchor = AnchorStyles.Bottom | AnchorStyles.Right
235 };
236 confirmation.Click += (sender, e) => { prompt.Close(); };
237 prompt.Controls.Add(textBox);
238 prompt.Controls.Add(confirmation);
239 prompt.Controls.Add(textLabel);
240 prompt.AcceptButton = confirmation;
241 prompt.Load += delegate
242 {
243 Theme.UseTheme(prompt);
244 DpiScaling.Apply(prompt);
245 new FormDimensions().Load(prompt, prompt.Owner);
246 };
247 if (monospaceFont)
248 {
249 textBox.SelectAll();
250 textBox.SelectionFont = new Font(FontFamily.GenericMonospace, prompt.Font.Size);
251 textBox.SelectionLength = 0;
252 }
253
254 prompt.KeyPress += (sender, e) =>
255 {
256 if (e.KeyChar == 27) prompt.Close();
257 };
258
259 DialogResult dialogResult = prompt.ShowDialog();
260 result = (dialogResult == DialogResult.OK) ? textBox.Text : "";
261 return dialogResult == DialogResult.OK;
262 }
263 }
264
265 public static class PromptOpenFile
266 {
267 public static bool ShowDialog(string fileName, string fileType, string fileExtension, out string result)
268 {
269 result = fileName;
270 OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = fileType + @"|*." +
271 fileExtension + @"|Any file|*.*", DefaultExt = fileExtension };
272 if (!string.IsNullOrEmpty(fileName))
273 {
274 openFileDialog.FileName = Path.GetFileName(fileName);
275 openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
276 }
277
278 DialogResult dialogResult = openFileDialog.ShowDialog();
279 if (dialogResult == DialogResult.OK)
280 {
281 result = openFileDialog.FileName;
282 }
283 return dialogResult == DialogResult.OK;
284 }
285 }
286
287 public static class PromptSaveFile
288 {
289 public static bool ShowDialog(string fileName, string fileType, string fileExtension, out string result)
290 {
291 result = fileName;
292 SaveFileDialog saveFileDialog = new SaveFileDialog
293 {
294 Filter = fileType + @"|*." +
295 fileExtension + @"|Any file|*.*",
296 DefaultExt = fileExtension
297 };
298 if (!string.IsNullOrEmpty(fileName))
299 {
300 saveFileDialog.FileName = Path.GetFileName(fileName);
301 saveFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
302 }
303
304 DialogResult dialogResult = saveFileDialog.ShowDialog();
305 if (dialogResult == DialogResult.OK)
306 {
307 result = saveFileDialog.FileName;
308 }
309 return dialogResult == DialogResult.OK;
310 }
311 }
312
313 public static class PromptOpenDir
314 {
315 public static bool ShowDialog(string dirName, out string result)
316 {
317 result = dirName;
318 FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog() { ShowNewFolderButton = true };
319 if (!string.IsNullOrEmpty(dirName))
320 folderBrowserDialog.SelectedPath = dirName;
321 DialogResult dialogResult = folderBrowserDialog.ShowDialog();
322 if (dialogResult == DialogResult.OK)
323 {
324 result = folderBrowserDialog.SelectedPath;
325 }
326 return dialogResult == DialogResult.OK;
327 }
328 }
329}
Note: See TracBrowser for help on using the repository browser.