69,369
社区成员
发帖
与我相关
我的任务
分享
A simple data entry dialog with functionally similar to a MessageBox.
Purpose: A simple data entry dialog functionally similar to a MessageBox. Any feedback is welcome.
Collapse | Copy Code
// Sample Utilization:
//Example 1
string s = string.Empty;
if (InputBox.ShowDialog("Type your name:", ref s) == DialogResult.Cancel) return;
//Example 2
int x = ((int?)InputBox.Create("Multiply by 2:", "1") ?? 0) * 2;
Here is the InputBox class:
Collapse | Copy Code
/// <summary>
/// Input dialog box used for simple user data entry.
/// </summary>
public static class InputBox
{
/// <summary>
/// Standard modal DialogResult method used for simple user input.
/// </summary>
/// <param name="caption">Title for the Input form.</param>
/// <param name="defaultValue">Default to be displayed in the textbox.</param>
/// <returns>DialogResult, and updates the value of reference parameter
/// defaultValue if the result is DialogResult.OK.</returns>
public static DialogResult ShowDialog(string caption, ref string defaultValue)
{
InputForm inForm = new InputForm(caption, defaultValue);
if (inForm.ShowDialog() == DialogResult.OK)
{
defaultValue = inForm.StringValue;
return DialogResult.OK;
}
return DialogResult.Cancel;
}
/// <summary>
/// Direct modal InputBox method, used for immediate typecasting.
/// Shows the dialog as part of its creation.
/// </summary>
/// <param name="caption">Title for the Input form.</param>
/// <param name="defaultValue">Default to be displayed in the textbox.</param>
/// <returns>InputForm ready to be typecast to the appropriate value type.</returns>
public static InputForm Create(string caption, string defaultValue)
{
InputForm inForm = new InputForm(caption, defaultValue);
if (inForm.ShowDialog() == DialogResult.Cancel) inForm.StringValue = string.Empty;
return inForm;
}
}
Here is the InputForm (used by InputBox):
Collapse | Copy Code
/// <summary>
/// Display class for InputBox. Should not be used directly, use InputBox instead.
/// </summary>
public partial class InputForm : Form
{
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
public InputForm()
{
InitializeComponent();
}
/// <summary>
/// Parameterized constructor.
/// </summary>
/// <param name="caption">Title for the Input form.</param>
/// <param name="defaultValue">Default to be displayed in the textbox.</param>
public InputForm(string caption, string defaultValue) : this()
{
this.Text = caption;
txtValue.Text = defaultValue;
}
#endregion Constructors
#region Public Properties
/// <summary>
/// Accessor for the textbox value.
/// </summary>
public string StringValue
{
get { return txtValue.Text; }
set { txtValue.Text = value; }
}
#endregion Public Properties
#region Typecasting Operators
/// <summary>
/// Typecasting operator.
/// </summary>
/// <param name="inForm">Completed InputForm with user entry.</param>
/// <returns>Nullable value: double.</returns>
public static explicit operator double?(InputForm inForm)
{
if (inForm.StringValue == string.Empty) return null;
try { return double.Parse(inForm.StringValue); }
catch { return null; }
}
/// <summary>
/// Typecasting operator.
/// </summary>
/// <param name="inForm">Completed InputForm with user entry.</param>
/// <returns>Nullable value: int.</returns>
public static explicit operator int?(InputForm inForm)
{
if (inForm.StringValue == string.Empty) return null;
try { return Int32.Parse(inForm.StringValue); }
catch { return null; }
}
/// <summary>
/// Typecasting operator.
/// </summary>
/// <param name="inForm">Completed InputForm with user entry.</param>
/// <returns>Nullable value: long.</returns>
public static explicit operator long?(InputForm inForm)
{
if (inForm.StringValue == string.Empty) return null;
try { return Int64.Parse(inForm.StringValue); }
catch { return null; }
}
/// <summary>
/// Typecasting operator.
/// </summary>
/// <param name="inForm">Completed InputForm with user entry.</param>
/// <returns>String value.</returns>
public static explicit operator string(InputForm inForm)
{
return inForm.StringValue;
}
#endregion Typecasting Operators
}