65.9K
CodeProject is changing. Read more.
Home

InputBox: A simple user input dialog

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (9 votes)

Nov 22, 2011

CPOL
viewsIcon

81551

A simple data entry dialog functionally similar to a MessageBox.

Purpose: A simple data entry dialog functionally similar to a MessageBox. Any feedback is welcome.

// Sample Utilization:

//Example 1
string s = string.Empty;
if (InputBox.ShowDialog("Type your name:", ref s) == DialogResult.Cancel) return;
 
//Example 2
int x = (InputBox.GetInt("Multiply by 2:", "1") ?? 0) * 2;

Here is the InputBox class:

/// <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 (as a string).
    /// </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)
    {
        using (InputForm inForm = new InputForm(caption, defaultValue))
        {
            if (inForm.ShowDialog() == DialogResult.OK)
            {
                defaultValue = inForm.StringValue;
                return DialogResult.OK;
            }
            return DialogResult.Cancel;
        }    
    }

    /// <summary>
    /// Prompts the user to provide a value as a double.
    /// </summary>
    /// <param name="caption">Title for the Input form.</param>
    /// <param name="defaultValue">Default to be displayed in the textbox.</param>
    /// <returns>Nullable value: double.</returns>
    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; }
        }
    }

    /// <summary>
    /// Prompts the user to provide a value as an int.
    /// </summary>
    /// <param name="caption">Title for the Input form.</param>
    /// <param name="defaultValue">Default to be displayed in the textbox.</param>
    /// <returns>Nullable value: int.</returns>
    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; }
        }
    }

    /// <summary>
    /// Prompts the user to provide a value as a long.
    /// </summary>
    /// <param name="caption">Title for the Input form.</param>
    /// <param name="defaultValue">Default to be displayed in the textbox.</param>
    /// <returns>Nullable value: long.</returns>
    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):

/// <summary>
/// Display class for InputBox.  Should not be used directly, use InputBox instead.
/// </summary>
internal partial class InputForm : Form
{
    #region Constructors
 
    /// <summary>
    /// Default constructor.
    /// </summary>
    internal 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>
    internal InputForm(string caption, string defaultValue) : this()
    {
        this.Text = caption;
        txtValue.Text = defaultValue;
    }
 
    #endregion Constructors
 
    #region Public Properties
 
    /// <summary>
    /// Accessor for the textbox value.
    /// </summary>
    internal string StringValue
    {
        get { return txtValue.Text; }
        set { txtValue.Text = value; }
    }
 
    #endregion Public Properties 
}