Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Input Box in C# for WindowsForms

0.00/5 (No votes)
24 Sep 2014 1  
Input box with TextBox or ComboBox inspired by classic MessageBox

With inputs:

Or like classical MessageBox:

Introduction

In C#, there isn't anything like InputBox. The only way how to use that is import Visual Basic library with that ugly one.

Using the Code

Description

  • Five languages (default English)
  • Four types of icon
  • Four possibilities for button option
  • Choose if you want TextBox, ComboBox or nothing (only message text)
  • Using string array for ComboBox items
  • Set font for message text
  • Set visibility in taskbar
  • Returns DialogResult
  • Returned string value is InputBox.ResultValue

Here is a sample of how to use:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MsgBox;

namespace InputBoxUsage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Set buttons language Czech/English/German/Slovakian/Spanish (default English)
            InputBox.SetLanguage(InputBox.Language.English);

            //Save the DialogResult as res
            DialogResult res = InputBox.ShowDialog("Select some item from ComboBox below:", 
            "Combo InputBox",   //Text message (mandatory), Title (optional)
                InputBox.Icon.Question, //Set icon type (default info)
                InputBox.Buttons.OkCancel, //Set buttons (default ok)
                InputBox.Type.TextBox, //Set type (default nothing)
                new string[] { "Item1","Item2","Item3"}, //String field as ComboBox items (default null)
                true, //Set visible in taskbar (default false)
                new System.Drawing.Font("Calibri", 10F, System.Drawing.FontStyle.Bold)); //Set font (default by system)
            
            //Check InputBox result
            if (res == System.Windows.Forms.DialogResult.OK || res == System.Windows.Forms.DialogResult.Yes)
                listView1.Items.Add(InputBox.ResultValue); //Get returned value
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.Columns.Add("Result", 100);
            listView1.View = View.Details;
        }
    }
}

//
//Source of InputBox class
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
namespace MsgBox
{
    public static class InputBox
    {
        private static System.Windows.Forms.Form frm = new System.Windows.Forms.Form();
        public static string ResultValue;
        private static DialogResult DialogRes;
        private static string[] buttonTextArray = new string[4];
        public enum Icon
        { 
            Error,
            Exclamation,
            Information,
            Question,
            Nothing
        }
        public enum Type
        { 
            ComboBox,
            TextBox,
            Nothing
        }
        public enum Buttons
        { 
            Ok,
            OkCancel,
            YesNo,
            YesNoCancel
        }
        public enum Language
        { 
            Czech,
            English,
            German,
            Slovakian,
            Spanish
        }
        /// <summary>
        /// This form is like a MessageBox, but you can select type of controls on it. 
        /// This form returning a DialogResult value.
        /// </summary>
        /// <param name="Message">Message in dialog(as System.String)</param>
        /// <param name="Title">Title of dialog (as System.String)</param>
        /// <param name="icon">Select icon (as InputBox.Icon)</param>
        /// <param name="buttons">Select icon (as InputBox.Buttons)</param>
        /// <param name="type">Type of control in Input box (as InputBox.Type)</param>
        /// <param name="ListItems">Array of ComboBox items (as System.String[])</param>
        /// <param name="FormFont">Font in form (as System.Drawing.Font)</param>
        /// <returns></returns>
        /// 
        public static DialogResult ShowDialog(string Message, string Title = "", 
        Icon icon = Icon.Information, Buttons buttons = Buttons.Ok, Type type = Type.Nothing, 
        string[] ListItems = null, bool ShowInTaskBar = false, Font FormFont = null)
        {
            frm.Controls.Clear();
            ResultValue = "";
            //Form definition
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            frm.Size = new System.Drawing.Size(350, 170);
            frm.Text = Title;
            frm.ShowIcon = false;
            frm.ShowInTaskbar = ShowInTaskBar;
            frm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(frm_FormClosing);
            frm.StartPosition = FormStartPosition.CenterParent;
            //Panel definition
            Panel panel = new Panel();
            panel.Location = new System.Drawing.Point(0, 0);
            panel.Size = new System.Drawing.Size(340, 97);
            panel.BackColor = System.Drawing.Color.White;
            frm.Controls.Add(panel);
            //Add icon in to panel
            panel.Controls.Add(Picture(icon));
            //Label definition (message)
            System.Windows.Forms.Label label = new System.Windows.Forms.Label();
            label.Text = Message;
            label.Size = new System.Drawing.Size(245, 60);
            label.Location = new System.Drawing.Point(90, 10);
            label.TextAlign = ContentAlignment.MiddleLeft;
            panel.Controls.Add(label);
            //Add buttons to the form
            foreach (Button btn in Btns(buttons))
                frm.Controls.Add(btn);
            //Add ComboBox or TextBox to the form
            Control ctrl = Cntrl(type, ListItems);
            panel.Controls.Add(ctrl);
            //Get automatically cursor to the TextBox
            if (ctrl.Name == "textBox")
                frm.ActiveControl = ctrl;
            //Set label font
            if (FormFont != null)
                label.Font = FormFont;
            frm.ShowDialog();
            //Return text value
            switch (type)
            {
                case Type.Nothing:
                    break;
                default:
                    if (DialogRes == DialogResult.OK || DialogRes == DialogResult.Yes)
                    { ResultValue = ctrl.Text; }
                    else ResultValue = "";
                    break;
            }
            return DialogRes;
        }
        private static void button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            switch (button.Name)
            { 
                case "Yes":
                    DialogRes = DialogResult.Yes;
                    break;
                case "No":
                    DialogRes = DialogResult.No;
                    break;
                case "Cancel":
                    DialogRes = DialogResult.Cancel;
                    break;
                default:
                    DialogRes = DialogResult.OK;
                    break;
            }
            frm.Close();
        }
        private static void textBox_KeyDown(object sender, System.Windows .Forms .KeyEventArgs  e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DialogRes = DialogResult.OK;
                frm.Close();
            }
        }
        private static void frm_FormClosing(object sender, System .Windows .Forms .FormClosingEventArgs  e)
        {
            if (DialogRes != null) { }
            else DialogRes = DialogResult.None;
        }
        private static PictureBox Picture(Icon icon)
        {
            System.Windows.Forms.PictureBox picture = new System.Windows.Forms.PictureBox();
            var assembly = Assembly.GetExecutingAssembly();     //Get integrated sources
            System.IO.Stream stream = null;
            //Set icon
            switch (icon)
            {
                case Icon.Error:
                    stream = assembly.GetManifestResourceStream(@"MsgBox.error.png");
                    break;
                case Icon.Exclamation:
                    stream = assembly.GetManifestResourceStream(@"MsgBox.exclamation.png");
                    break;
                case Icon.Information:
                    stream = assembly.GetManifestResourceStream(@"MsgBox.information.png");
                    break;
                case Icon.Question:
                    stream = assembly.GetManifestResourceStream(@"MsgBox.question.png");
                    break;
                case Icon.Nothing:
                    stream = assembly.GetManifestResourceStream(@"MsgBox.nic80x80.png");
                    break;
            }
            picture.Image = System.Drawing.Image.FromStream(stream);
            picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            picture.Size = new System.Drawing.Size(60, 60);
            picture.Location = new System.Drawing.Point(10, 10);
            return picture;
        }
       private static Button[] Btns(Buttons button, Language lang = Language.English)
        {
            //Buttons field for return
            System.Windows.Forms.Button[] returnButtons = new Button[3];
            //Buttons instances
            System.Windows.Forms.Button OkButton = new System.Windows.Forms.Button();
            System.Windows.Forms.Button StornoButton = new System.Windows.Forms.Button();
            System.Windows.Forms.Button AnoButton = new System.Windows.Forms.Button();
            System.Windows.Forms.Button NeButton = new System.Windows.Forms.Button();
            //Set buttons names and text
            OkButton.Text = buttonTextArray[0];
            OkButton.Name = "OK";
            AnoButton.Text = buttonTextArray[1];
            AnoButton.Name = "Yes";
            NeButton.Text = buttonTextArray[2];
            NeButton.Name = "No";
            StornoButton.Text = buttonTextArray[3];
            StornoButton.Name = "Cancel";
            //Set buttons position
            switch (button)
            {
                case Buttons.Ok:
                    OkButton.Location = new System.Drawing.Point(250, 101);
                    returnButtons[0] = OkButton;
                    break;
                case Buttons.OkCancel:
                    OkButton.Location = new System.Drawing.Point(170, 101);
                    returnButtons[0] = OkButton;
                    StornoButton.Location = new System.Drawing.Point(250, 101);
                    returnButtons[1] = StornoButton;
                    break;
                case Buttons.YesNo:
                    AnoButton.Location = new System.Drawing.Point(170, 101);
                    returnButtons[0] = AnoButton;
                    NeButton.Location = new System.Drawing.Point(250, 101);
                    returnButtons[1] = NeButton;
                    break;
                case Buttons.YesNoCancel:
                    AnoButton.Location = new System.Drawing.Point(90, 101);
                    returnButtons[0] = AnoButton;    
                    NeButton.Location = new System.Drawing.Point(170, 101);
                    returnButtons[1] = NeButton;
                    StornoButton.Location = new System.Drawing.Point(250, 101);
                    returnButtons[2] = StornoButton;
                    break;
            }
            //Set size and event for all used buttons
            foreach (Button btn in returnButtons)
            {
                if (btn != null)
                {
                    btn.Size = new System.Drawing.Size(75, 23);
                    btn.Click += new System.EventHandler(button_Click);
                }
            }
            return returnButtons;
        }
        private static Control Cntrl(Type type, string[] ListItems)
        {
            //ComboBox
            System.Windows.Forms.ComboBox comboBox = new System.Windows.Forms.ComboBox();
            comboBox.Size = new System.Drawing.Size(180, 22);
            comboBox.Location = new System.Drawing.Point(90, 70);
            comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            comboBox.Name = "comboBox";
            if (ListItems != null)
            {
                foreach (string item in ListItems)
                    comboBox.Items.Add(item);
                comboBox.SelectedIndex = 0;
            }
            //Textbox
            System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
            textBox.Size = new System.Drawing.Size(180, 23);
            textBox.Location = new System.Drawing.Point(90, 70);
            textBox.KeyDown += new System.Windows.Forms.KeyEventHandler(textBox_KeyDown);
            textBox.Name = "textBox";
            //Set returned Control
            Control returnControl = new Control();
            switch (type)
            {
                case Type.ComboBox:
                    returnControl = comboBox;
                    break;
                case Type.TextBox:
                    returnControl = textBox;
                    break;
            }
            return returnControl;
        }
        public static void SetLanguage(Language lang)
        {
            switch (lang)
            {
                case Language.Czech:
                    buttonTextArray = "OK,Ano,Ne,Storno".Split(',');
                    break;
                case Language.German:
                    buttonTextArray = "OK,Ja,Nein,Stornieren".Split(',');
                    break;
                case Language.Spanish:
                    buttonTextArray = "OK,S&iacute;,No,Cancelar".Split(',');
                    break;
                case Language.Slovakian:
                    buttonTextArray = "OK,&Aacute;no,Nie,Zru&scaron;it".Split(',');
                    break;
                default:
                    buttonTextArray = "OK,Yes,No,Cancel".Split(',');
                    break;
            }
        }
    }
}

The code is written in a very simple way, nothing special. InputBox is a static class to avoid the need to create a new instance in every use.

First is form defined. On the form is Panel object which contains other controls but buttons.

  • Icon is defined by Icon Enum. Pictures are compiled as integrated source.
  • Buttons are defined by Buttons Enum. Function Btns returns Buttons in array. The language of buttons text is defined by Language Enum.
  • Function Cntrl returns TextBox or ComboBox by Type enum. If Type.Nothing is selected, InputBox will not have any input control. ResultValue will be "". TextBox has assigned KeyPress event for Enter key that fills ResultValue as TextBox.Text, DialogResult as OK and closes the form.
  • The form returns classic DialogResult like a MessageBox.

The source code is in the attachment (VS2012) and can be downloaded from the link at the top.

I hope it's a useful solution and it helps somebody.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here