Simple Input Dialog In C#

Have you ever tried looking for a Input Dialog box in C#? I did recently and fully expecting something similar to the MessageBox.Show() found out that there wasn’t one. So I browsed the web for a quick solution, and did’nt find exactly what I wanted so I decided to make my own. Here is how I did it for those of you facing the same problem.

Just to be clear, my goals were :

  • Simple looking dialog, with caption, description, and a textbox.
  • Option to press ok and also cancel out.
  • Easy way to return the value.

Okay lets start, create a form named InputDialog.cs with the following code in InputDialog.Designer.cs but make sure you rename the namespace to your own :

namespace InputDialogExample    // <--- you must change this!
{
	partial class InputDialog
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tblLayout = new System.Windows.Forms.TableLayoutPanel();
			this.txtInput = new System.Windows.Forms.TextBox();
			this.lblInput = new System.Windows.Forms.Label();
			this.btnOk = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.tblLayout.SuspendLayout();
			this.SuspendLayout();
			//
			// tblLayout
			//
			this.tblLayout.ColumnCount = 2;
			this.tblLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
			this.tblLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
			this.tblLayout.Controls.Add(this.lblInput, 0, 0);
			this.tblLayout.Controls.Add(this.txtInput, 1, 0);
			this.tblLayout.Location = new System.Drawing.Point(12, 12);
			this.tblLayout.Name = "tblLayout";
			this.tblLayout.RowCount = 1;
			this.tblLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
			this.tblLayout.Size = new System.Drawing.Size(222, 27);
			this.tblLayout.TabIndex = 0;
			//
			// txtInput
			//
			this.txtInput.Dock = System.Windows.Forms.DockStyle.Fill;
			this.txtInput.Location = new System.Drawing.Point(50, 3);
			this.txtInput.Name = "txtInput";
			this.txtInput.Size = new System.Drawing.Size(169, 21);
			this.txtInput.TabIndex = 0;
			this.txtInput.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged);
			//
			// lblInput
			//
			this.lblInput.AutoSize = true;
			this.lblInput.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lblInput.Location = new System.Drawing.Point(3, 0);
			this.lblInput.Name = "lblInput";
			this.lblInput.Size = new System.Drawing.Size(41, 27);
			this.lblInput.TabIndex = 1;
			this.lblInput.Text = "Name :";
			this.lblInput.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			//
			// btnOk
			//
			this.btnOk.AutoSize = true;
			this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOk.Enabled = false;
			this.btnOk.Location = new System.Drawing.Point(127, 48);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new System.Drawing.Size(41, 26);
			this.btnOk.TabIndex = 1;
			this.btnOk.Text = "&OK";
			this.btnOk.UseVisualStyleBackColor = true;
			this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
			//
			// btnCancel
			//
			this.btnCancel.AutoSize = true;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(174, 48);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(57, 26);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "&Cancel";
			this.btnCancel.UseVisualStyleBackColor = true;
			//
			// InputDialog
			//
			this.AcceptButton = this.btnOk;
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(243, 85);
			this.ControlBox = false;
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOk);
			this.Controls.Add(this.tblLayout);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "InputDialog";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = " Input Dialog";
			this.tblLayout.ResumeLayout(false);
			this.tblLayout.PerformLayout();
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.TableLayoutPanel tblLayout;
		private System.Windows.Forms.Label lblInput;
		private System.Windows.Forms.TextBox txtInput;
		private System.Windows.Forms.Button btnOk;
		private System.Windows.Forms.Button btnCancel;
	}
}

Now you should have a form that looks like this :

Newly created input dialog

Newly created input dialog

Now this form needs some code behind it to function, so clear everything behind it and paste the following :

using System;
using System.Windows.Forms;

namespace InputDialogExample  // <--- you must change this!
{
	public partial class InputDialog : Form
	{
		string input_text;

		public string ResultText
		{
			get { return input_text; }
			private set { input_text = value; }
		}

		public InputDialog(string title, string label_text, string textbox_string)
		{
			InitializeComponent();
			this.Text = title;
			this.lblInput.Text = label_text;
			this.txtInput.Text = textbox_string;
		}

		public InputDialog()
		{
			InitializeComponent();
		}

		private void txtInput_TextChanged(object sender, EventArgs e)
		{
			if (txtInput.Text.Trim().Length > 0)
			{
				btnOk.Enabled = true;
			}
			else
			{
				btnOk.Enabled = false;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			ResultText = txtInput.Text.Trim();
		}
	}
}

Congratulations! The input dialog is done. Use the code below to show it, and fetch the result from it :

InputDialog dialog = new InputDialog("Caption Here", "Label Text Here", "Default Textbox String");
if (dialog.ShowDialog() == DialogResult.OK)
{
    string result_text = dialog.ResultText;
    // use result_text...
}
else
{
    // user cancelled out, do something...
}

Thats it! Inshaa’Allah (God Willing) this article was helpful to you.

— Muhammad

This entry was posted in C# Tutorials and tagged , , , , . Bookmark the permalink.

Leave a comment