Purpose: A simple data entry dialog functionally similar to a MessageBox
. Any feedback is welcome.
string s = string.Empty;
if (InputBox.ShowDialog("Type your name:", ref s) == DialogResult.Cancel) return;
int x = (InputBox.GetInt("Multiply by 2:", "1") ?? 0) * 2;
Here is the InputBox
class:
public static class InputBox
{
public static DialogResult ShowDialog(string caption, ref string defaultValue)
{
using (InputForm inForm = new InputForm(caption, defaultValue))
{
if (inForm.ShowDialog() == DialogResult.OK)
{
defaultValue = inForm.StringValue;
return DialogResult.OK;
}
return DialogResult.Cancel;
}
}
public static double? GetDouble(string caption, string defaultValue)
{
using (InputForm inForm = new InputForm(caption, defaultValue))
{
if (inForm.ShowDialog() == DialogResult.Cancel) return null;
if (inForm.StringValue == string.Empty) return null;
try { return double.Parse(inForm.StringValue); }
catch { return null; }
}
}
public static int? GetInt(string caption, string defaultValue)
{
using (InputForm inForm = new InputForm(caption, defaultValue))
{
if (inForm.ShowDialog() == DialogResult.Cancel) return null;
if (inForm.StringValue == string.Empty) return null;
try { return Int32.Parse(inForm.StringValue); }
catch { return null; }
}
}
public static long? GetLong(string caption, string defaultValue)
{
using (InputForm inForm = new InputForm(caption, defaultValue))
{
if (inForm.ShowDialog() == DialogResult.Cancel) return null;
if (inForm.StringValue == string.Empty) return null;
try { return Int64.Parse(inForm.StringValue); }
catch { return null; }
}
}
}
Here is the InputForm
(used by InputBox
):
internal partial class InputForm : Form
{
#region Constructors
internal InputForm()
{
InitializeComponent();
}
internal InputForm(string caption, string defaultValue) : this()
{
this.Text = caption;
txtValue.Text = defaultValue;
}
#endregion Constructors
#region Public Properties
internal string StringValue
{
get { return txtValue.Text; }
set { txtValue.Text = value; }
}
#endregion Public Properties
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.