using System;
using System.Drawing;//for use of Point
using System.Windows.Forms;

/// <summary>
/// Displayes a prompt in a dialog box, waits for the user to input text or click a
/// button, and then returns a string containing the contents of the text box.
/// </summary>
public class InputBox
{
	/// <summary>
	/// This field contains the return string
	/// which user has entered.
	/// </summary>
	private static string returnValue = String.Empty;

	/// <summary>
	/// Show the InputBox to user.
	/// </summary>
	/// <param name="prompt">String expression displayed as the message in the dialog box.</param>
	/// <param name="title">String expression displayed in the title bar of the dialog box.
	/// If you omit Title, an empty string is placed in the title bar.</param>
	/// <returns>Returns the user-entered text.</returns>
	public static string Show(string prompt,string title)
	{	
		InputBoxForm frm = new InputBoxForm(prompt,title);
		frm.Closing += new System.ComponentModel.CancelEventHandler(SetReturnValue);
		frm.ShowDialog();
		
		return returnValue;
	}
	/// <summary>
	/// Show the InputBox to user.
	/// </summary>
	/// <param name="prompt">String expression displayed as the message in the dialog box.</param>
	/// <returns>Returns the user-entered text.</returns>
	public static string Show(string prompt)
	{
		return Show(prompt,"");
	}

	/// <summary>
	/// This class is the InputBox.
	/// </summary>
	private class InputBoxForm:Form
	{
		private  Button btnOk;
		private  Button btnCancel;
		private TextBox txt;
		private  Label lbl;

		private string prompt;
		private string title;
		
		public InputBoxForm(string prompt,string title)
		{
			this.title = title;
			this.prompt = prompt;
			InitializeComponent();
		}

		private void InitializeComponent()
		{
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.ShowInTaskbar = false;
			this.Size = new Size(360,150);
			this.StartPosition = FormStartPosition.CenterParent;
			this.Text = title;
			this.FormBorderStyle = FormBorderStyle.FixedDialog;
			this.SuspendLayout();

			btnOk = new Button();
			btnCancel = new Button();
			txt  = new TextBox();
			lbl = new Label();
            

			//btnOk
			btnOk.Click += new EventHandler(btnOk_Click);
			btnOk.FlatStyle = FlatStyle.System;
			btnOk.Location = new Point(280,8);
			btnOk.Size = new Size(64,24);
			btnOk.TabIndex = 1;
			btnOk.Text = "OK";

			//btnCancel
			btnCancel.Click += new EventHandler(btnCancel_Click);
			btnCancel.FlatStyle = FlatStyle.System;
			btnCancel.Location = new Point(280,38);
			btnCancel.Size = new Size(64,24);
			btnCancel.TabIndex = 2;
			btnCancel.Text = "Cancel";

			//lbl
			lbl.Location = new Point(8,8);
			lbl.Size = new Size(254,48);
			lbl.Text = prompt;
          
			//txt
			txt.Location = new Point(8,88);
			txt.Size = new Size(336,20);
			txt.TabIndex = 0;
			txt.Text = String.Empty;

			Control[] ctrls = {btnOk,btnCancel,txt,lbl};
			this.Controls.AddRange(ctrls);
			this.ResumeLayout();
			this.AcceptButton = btnOk;
			this.CancelButton = btnCancel;
		}

		public  string returnValue = String.Empty;

		private  void btnOk_Click(object sender, EventArgs e)
		{
			returnValue = txt.Text;
			this.Close();
		}

		private  void btnCancel_Click(object sender, EventArgs e)
		{
			returnValue = String.Empty;
			this.Close();
		}
	}

	private static void SetReturnValue(object sender, System.ComponentModel.CancelEventArgs e)
	{
		returnValue = ((InputBoxForm)sender).returnValue;
	}
}