using System; using System.IO; using System.Drawing; using System.Windows.Forms; namespace Common { public static class PromptText { public static bool ShowDialog(string text, string caption, string initialValue, out string result) { Form prompt = new Form() { Width = 500, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen, }; Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; TextBox textBox = new TextBox() { Left = 50, Top = 44, Width = 400, Text = initialValue }; textBox.SelectAll(); Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK }; confirmation.Click += (sender, e) => { prompt.Close(); }; prompt.Controls.Add(textBox); prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.AcceptButton = confirmation; prompt.Load += delegate { Theme.UseTheme(prompt); DpiScaling.Apply(prompt); new FormDimensions().Load(prompt, prompt.Owner); }; prompt.KeyPress += (sender, e) => { if (e.KeyChar == 27) prompt.Close(); }; DialogResult dialogResult = prompt.ShowDialog(); result = (dialogResult == DialogResult.OK) ? textBox.Text : ""; return dialogResult == DialogResult.OK; } } public static class PromptNumber { public static bool ShowDialog(string text, string caption, int initialValue, out int result) { Form prompt = new Form() { Width = 500, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; NumericUpDown numericUpDown = new NumericUpDown() { Left = 50, Top = 44, Width = 80, Maximum = 100000, Minimum = 0, Value = Math.Min(Math.Max(initialValue, 0), 100000) }; numericUpDown.Select(0, numericUpDown.Text.Length); Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK }; confirmation.Click += (sender, e) => { prompt.Close(); }; prompt.Controls.Add(numericUpDown); prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.AcceptButton = confirmation; prompt.Load += delegate { Theme.UseTheme(prompt); DpiScaling.Apply(prompt); }; numericUpDown.KeyPress += (sender, e) => { if (e.KeyChar == 27) prompt.Close(); }; DialogResult dialogResult = prompt.ShowDialog(); result = (dialogResult == DialogResult.OK) ? (int)numericUpDown.Value : -1; return dialogResult == DialogResult.OK; } } public static class PromptComboBox { public static bool ShowDialog(string text, string caption, object[] states, object initialValue, out object result) { Form prompt = new Form() { Width = 500, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; ComboBox comboBox = new ComboBox() { Left = 50, Top = 44, Width = 300, DropDownStyle = ComboBoxStyle.DropDownList }; comboBox.Items.AddRange(states); int itemIndex = comboBox.Items.IndexOf(initialValue); if (itemIndex >= 0) { comboBox.SelectedIndex = itemIndex; } else if (comboBox.Items.Count > 0) { comboBox.SelectedIndex = 0; } Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK }; confirmation.Click += (sender, e) => { prompt.Close(); }; prompt.Controls.Add(comboBox); prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.AcceptButton = confirmation; prompt.Load += delegate { Theme.UseTheme(prompt); DpiScaling.Apply(prompt); }; comboBox.KeyPress += (sender, e) => { if (e.KeyChar == 27) prompt.Close(); }; DialogResult dialogResult = prompt.ShowDialog(); result = (dialogResult == DialogResult.OK) ? comboBox.SelectedItem : null; return dialogResult == DialogResult.OK; } } public static class PromptListBox { public static bool ShowDialog(string text, string caption, object[] states, object initialValue, out object result) { int dataSize = 200; Form prompt = new Form() { Width = 500, Height = 150 + dataSize, FormBorderStyle = FormBorderStyle.Sizable, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = 70 + dataSize, DialogResult = DialogResult.OK, Anchor = AnchorStyles.Bottom | AnchorStyles.Right }; confirmation.Click += (sender, e) => { prompt.Close(); }; ListBox listBox = new ListBox() { Left = 50, Top = 44, Width = 400, Height = dataSize, Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left }; listBox.SelectedIndexChanged += (sender, e) => { confirmation.Enabled = listBox.SelectedItems.Count > 0; }; listBox.Items.AddRange(states); confirmation.Enabled = listBox.SelectedItems.Count > 0; listBox.DoubleClick += (sender, e) => { confirmation.PerformClick(); }; int itemIndex = -1; if (initialValue != null) itemIndex = listBox.Items.IndexOf(initialValue); if (itemIndex >= 0) { listBox.SelectedIndex = itemIndex; } else if (listBox.Items.Count > 0) { listBox.SelectedIndex = 0; } prompt.Controls.Add(listBox); prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.AcceptButton = confirmation; prompt.Load += delegate { Theme.UseTheme(prompt); DpiScaling.Apply(prompt); }; listBox.KeyPress += (sender, e) => { if (e.KeyChar == 27) prompt.Close(); }; DialogResult dialogResult = prompt.ShowDialog(); result = (dialogResult == DialogResult.OK) ? listBox.SelectedItem : null; return dialogResult == DialogResult.OK; } } public static class PromptRichTextBox { public static bool ShowDialog(string text, string caption, string initialValue, out string result, bool monospaceFont = false) { int dataSize = 200; Form prompt = new Form() { Width = 500, Height = 150 + dataSize, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen, }; Label textLabel = new Label() { Left = 50, Top = 20, AutoSize = true, Text = text }; RichTextBox textBox = new RichTextBox() { Left = 50, Top = 44, Width = 400, Height = dataSize, Text = initialValue }; textBox.TextChanged += delegate { Theme.UseTheme(prompt); }; textBox.SelectAll(); Button confirmation = new Button() { Text = @"Ok", Left = 350, Width = 100, Top = dataSize + 70, DialogResult = DialogResult.OK }; confirmation.Click += (sender, e) => { prompt.Close(); }; prompt.Controls.Add(textBox); prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.AcceptButton = confirmation; prompt.Load += delegate { Theme.UseTheme(prompt); DpiScaling.Apply(prompt); new FormDimensions().Load(prompt, prompt.Owner); }; if (monospaceFont) { textBox.SelectAll(); textBox.SelectionFont = new Font(FontFamily.GenericMonospace, prompt.Font.Size); textBox.SelectionLength = 0; } prompt.KeyPress += (sender, e) => { if (e.KeyChar == 27) prompt.Close(); }; DialogResult dialogResult = prompt.ShowDialog(); result = (dialogResult == DialogResult.OK) ? textBox.Text : ""; return dialogResult == DialogResult.OK; } } public static class PromptOpenFile { public static bool ShowDialog(string fileName, string fileType, string fileExtension, out string result) { result = fileName; OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = fileType + @"|*." + fileExtension + @"|Any file|*.*", DefaultExt = fileExtension }; if (!string.IsNullOrEmpty(fileName)) { openFileDialog.FileName = Path.GetFileName(fileName); openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); } DialogResult dialogResult = openFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { result = openFileDialog.FileName; } return dialogResult == DialogResult.OK; } } public static class PromptSaveFile { public static bool ShowDialog(string fileName, string fileType, string fileExtension, out string result) { result = fileName; SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = fileType + @"|*." + fileExtension + @"|Any file|*.*", DefaultExt = fileExtension }; if (!string.IsNullOrEmpty(fileName)) { saveFileDialog.FileName = Path.GetFileName(fileName); saveFileDialog.InitialDirectory = Path.GetDirectoryName(fileName); } DialogResult dialogResult = saveFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { result = saveFileDialog.FileName; } return dialogResult == DialogResult.OK; } } public static class PromptOpenDir { public static bool ShowDialog(string dirName, out string result) { result = dirName; FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog() { ShowNewFolderButton = true }; if (!string.IsNullOrEmpty(dirName)) folderBrowserDialog.SelectedPath = dirName; DialogResult dialogResult = folderBrowserDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { result = folderBrowserDialog.SelectedPath; } return dialogResult == DialogResult.OK; } } }