C# InputBox

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace input
{
    public class InputBox : Form
    {
        private Container components = null;

        private Label label;
        private TextBox textbox;
        private Button button;

        private string input_value = "";

        public InputBox(string prompt) : this(prompt, "", ""){}

        public InputBox(string prompt, string title) : this(prompt, title, "") {}

        public InputBox(string prompt, string title, string default_value)
        {
            Init();
            set(prompt, title, default_value);
        }

        private void set(string prompt, string title, string default_value)
        {
            this.label.Text = prompt;
            this.Text = title;
            this.textbox.Text = default_value;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                if (components != null)
                    components.Dispose();
            base.Dispose(disposing);
        }

        private void Init()
        {
            this.label = new Label();
            this.textbox = new TextBox();
            this.button = new Button();

            this.SuspendLayout();

            this.label.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.label.Location = new Point(10, 10);
            this.label.Size = new Size(190, 20);

            this.textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            this.textbox.Location = new Point(10, 35);
            this.textbox.Size = new Size(150, 25);
            this.textbox.KeyPress += new KeyPressEventHandler(textbox_KeyPress);

            this.button.Anchor = AnchorStyles.Right;
            this.button.Location = new Point(170, 33);                        
            this.button.Text = "OK";            
            this.button.Click += new System.EventHandler(this.button_Click);

            Controls.Add(this.label);
            Controls.Add(this.textbox);
            Controls.Add(this.button);

            // enable the escape key
            this.CancelButton = null;
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(InputBox_KeyPress);

            this.Size = new Size(260, 100);
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.Name = "InputBox";

            this.ResumeLayout(false);
        }

        void InputBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.Value = null;
                this.Close();
            }
        }

        void textbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
                this.button.PerformClick();
        }


        private void button_Click(object sender, EventArgs e)
        {
            this.Value = this.textbox.Text;
            this.Close();
        }

        public string Value
        {
            get { return this.input_value; }
            set { this.input_value = value; }
        }

        public string Show()
        {
            this.ShowDialog();
            return this.Value;
        }

        public string Show(string prompt)
        {
            return Show(prompt, "", "");
        }

        public string Show(string prompt, string title)
        {
            return Show(prompt, title, "");
        }

        public string Show(string prompt, string title, string default_value)
        {
            set(prompt, title, default_value);
            return Show();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s