Click here to Skip to main content
15,885,874 members
Articles / Programming Languages / C#
Tip/Trick

InputBox: A simple user input dialog

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
25 Dec 2011CPOL 80.3K   5   4
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.


C#
// 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:


C#
/// <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):


C#
/// <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 
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionGot it working adding a couple of steps Pin
_codingfab30-Aug-18 21:42
_codingfab30-Aug-18 21:42 
GeneralReason for my vote of 3 a good idea Pin
zf.liu1-Dec-11 3:56
zf.liu1-Dec-11 3:56 
QuestionYou might consider using the TrySomething(out value) pattern. Pin
Kabwla.Phone1-Dec-11 7:32
Kabwla.Phone1-Dec-11 7:32 
SuggestionI like how you have adapted your code. Pin
Kabwla.Phone30-Nov-11 21:18
Kabwla.Phone30-Nov-11 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.