Skip to content

Instantly share code, notes, and snippets.

@chsbuffer
Created June 12, 2024 03:46
Show Gist options
  • Save chsbuffer/514151aac65fac4c02763a5f8006e7a3 to your computer and use it in GitHub Desktop.
Save chsbuffer/514151aac65fac4c02763a5f8006e7a3 to your computer and use it in GitHub Desktop.
C# WinForms InputDialogs (Generated by ChatGPT 4o)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
public static class InputDialogs
{
public static double getDouble(out bool ok, string title, string label, double value = 0, double min = -2147483647, double max = 2147483647, int decimals = 1, double step = 1, IWin32Window parent = null)
{
double result = value;
ok = false;
using (Form form = new Form())
{
form.Text = title;
form.StartPosition = FormStartPosition.CenterParent;
form.Font = new Font(form.Font.FontFamily, 10);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.ClientSize = new Size(300, 140);
Label textLabel = new Label() { Text = label, Left = 10, Top = 10, AutoSize = true };
NumericUpDown numericUpDown = new NumericUpDown()
{
Left = 10,
Top = 40,
Width = 260,
DecimalPlaces = decimals,
Minimum = (decimal)min,
Maximum = (decimal)max,
Value = (decimal)value,
Increment = (decimal)step
};
Button confirmation = new Button() { Text = "OK", Left = 110, Width = 80, Top = 80, DialogResult = DialogResult.OK };
Button cancel = new Button() { Text = "Cancel", Left = 200, Width = 80, Top = 80, DialogResult = DialogResult.Cancel };
form.Controls.Add(textLabel);
form.Controls.Add(numericUpDown);
form.Controls.Add(confirmation);
form.Controls.Add(cancel);
form.AcceptButton = confirmation;
form.CancelButton = cancel;
if (form.ShowDialog(parent) == DialogResult.OK)
{
result = (double)numericUpDown.Value;
ok = true;
}
}
return result;
}
public static int getInt(out bool ok, string title, string label, int value = 0, int min = -2147483647, int max = 2147483647, int step = 1, IWin32Window parent = null)
{
int result = value;
ok = false;
using (Form form = new Form())
{
form.Text = title;
form.StartPosition = FormStartPosition.CenterParent;
form.Font = new Font(form.Font.FontFamily, 10);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.ClientSize = new Size(300, 140);
Label textLabel = new Label() { Text = label, Left = 10, Top = 10, AutoSize = true };
NumericUpDown numericUpDown = new NumericUpDown()
{
Left = 10,
Top = 40,
Width = 260,
Minimum = min,
Maximum = max,
Value = value,
Increment = step
};
Button confirmation = new Button() { Text = "OK", Left = 110, Width = 80, Top = 80, DialogResult = DialogResult.OK };
Button cancel = new Button() { Text = "Cancel", Left = 200, Width = 80, Top = 80, DialogResult = DialogResult.Cancel };
form.Controls.Add(textLabel);
form.Controls.Add(numericUpDown);
form.Controls.Add(confirmation);
form.Controls.Add(cancel);
form.AcceptButton = confirmation;
form.CancelButton = cancel;
if (form.ShowDialog(parent) == DialogResult.OK)
{
result = (int)numericUpDown.Value;
ok = true;
}
}
return result;
}
public static string getItem(out bool ok, string title, string label, List<string> items, int current = 0, bool editable = true, IWin32Window parent = null)
{
string result = items.Count > 0 ? items[current] : string.Empty;
ok = false;
using (Form form = new Form())
{
form.Text = title;
form.StartPosition = FormStartPosition.CenterParent;
form.Font = new Font(form.Font.FontFamily, 10);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.ClientSize = new Size(300, 140);
Label textLabel = new Label() { Text = label, Left = 10, Top = 10, AutoSize = true };
ComboBox comboBox = new ComboBox()
{
Left = 10,
Top = 40,
Width = 260,
DropDownStyle = editable ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList
};
comboBox.Items.AddRange(items.ToArray());
comboBox.SelectedIndex = current;
Button confirmation = new Button() { Text = "OK", Left = 110, Width = 80, Top = 80, DialogResult = DialogResult.OK };
Button cancel = new Button() { Text = "Cancel", Left = 200, Width = 80, Top = 80, DialogResult = DialogResult.Cancel };
form.Controls.Add(textLabel);
form.Controls.Add(comboBox);
form.Controls.Add(confirmation);
form.Controls.Add(cancel);
form.AcceptButton = confirmation;
form.CancelButton = cancel;
if (form.ShowDialog(parent) == DialogResult.OK)
{
result = comboBox.Text;
ok = true;
}
}
return result;
}
public static string getMultiLineText(out bool ok, string title, string label, string text = "", IWin32Window parent = null)
{
string result = text;
ok = false;
using (Form form = new Form())
{
form.Text = title;
form.StartPosition = FormStartPosition.CenterParent;
form.Font = new Font(form.Font.FontFamily, 10);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.ClientSize = new Size(450, 300);
Label textLabel = new Label() { Text = label, Left = 10, Top = 10, AutoSize = true };
TextBox textBox = new TextBox()
{
Left = 10,
Top = 40,
Width = 420,
Height = 200,
Multiline = true,
Text = text,
ScrollBars = ScrollBars.Both
};
Button confirmation = new Button() { Text = "OK", Left = 250, Width = 80, Top = 250, DialogResult = DialogResult.OK };
Button cancel = new Button() { Text = "Cancel", Left = 350, Width = 80, Top = 250, DialogResult = DialogResult.Cancel };
form.Controls.Add(textLabel);
form.Controls.Add(textBox);
form.Controls.Add(confirmation);
form.Controls.Add(cancel);
form.AcceptButton = confirmation;
form.CancelButton = cancel;
if (form.ShowDialog(parent) == DialogResult.OK)
{
result = textBox.Text;
ok = true;
}
}
return result;
}
public static string getText(out bool ok, string title, string label, char passwordChar = '\0', string text = "", ImeMode imeMode = ImeMode.NoControl, IWin32Window parent = null)
{
string result = text;
ok = false;
using (Form form = new Form())
{
form.Text = title;
form.StartPosition = FormStartPosition.CenterParent;
form.Font = new Font(form.Font.FontFamily, 10);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.ClientSize = new Size(300, 140);
Label textLabel = new Label() { Text = label, Left = 10, Top = 10, AutoSize = true };
TextBox textBox = new TextBox()
{
Left = 10,
Top = 40,
Width = 260,
Text = text,
UseSystemPasswordChar = passwordChar != '\0',
ImeMode = imeMode
};
Button confirmation = new Button() { Text = "OK", Left = 110, Width = 80, Top = 80, DialogResult = DialogResult.OK };
Button cancel = new Button() { Text = "Cancel", Left = 200, Width = 80, Top = 80, DialogResult = DialogResult.Cancel };
form.Controls.Add(textLabel);
form.Controls.Add(textBox);
form.Controls.Add(confirmation);
form.Controls.Add(cancel);
form.AcceptButton = confirmation;
form.CancelButton = cancel;
if (form.ShowDialog(parent) == DialogResult.OK)
{
result = textBox.Text;
ok = true;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment