Click here to Skip to main content
Click here to Skip to main content

InputBox: A simple user input dialog

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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 
}

License

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

About the Author

Paladin2000

United States United States
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 3 a good idea Pinmemberzf.liu1-Dec-11 3:56 
QuestionYou might consider using the TrySomething(out value) pattern. PinmemberKabwla.Phone1-Dec-11 7:32 
SuggestionI like how you have adapted your code. PinmemberKabwla.Phone30-Nov-11 21:18 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Paladin2000
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid