|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace VainWebSpider
{
#region InputBoxDialog CLASS
/// <summary>
/// Provides a generic modal text input box, for use with
/// any other form
/// </summary>
public class InputBoxDialog : System.Windows.Forms.Form
{
#region Instance Fields
//instance fields
string formCaption = string.Empty;
string formPrompt = string.Empty;
string inputResponse = string.Empty;
string defaultValue = string.Empty;
private System.Windows.Forms.Label lblPrompt;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.TextBox txtInput;
private System.ComponentModel.Container components = null;
#endregion
#region Constructor
/// <summary>
/// Constructs a new InputBoxDialog object
/// </summary>
public InputBoxDialog()
{
InitializeComponent();
}
#endregion
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.lblPrompt = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.txtInput = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lblPrompt
//
this.lblPrompt.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lblPrompt.BackColor = System.Drawing.SystemColors.Control;
this.lblPrompt.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblPrompt.Location = new System.Drawing.Point(9, 35);
this.lblPrompt.Name = "lblPrompt";
this.lblPrompt.Size = new System.Drawing.Size(302, 22);
this.lblPrompt.TabIndex = 3;
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(265, 59);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(60, 20);
this.btnOK.TabIndex = 1;
this.btnOK.Text = "Ok";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(331, 59);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(60, 20);
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(8, 59);
this.txtInput.MaxLength = 40;
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(251, 20);
this.txtInput.TabIndex = 0;
//
// InputBoxDialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(398, 103);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.lblPrompt);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBoxDialog";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "InputBox";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.InputBoxDialog_KeyDown);
this.Load += new System.EventHandler(this.InputBox_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#region Dispose
/// <summary>
/// Clean up any resources being used.
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion
#endregion
#region Public Properties
// property FormCaption
public string FormCaption
{
get { return formCaption; }
set { formCaption = value; }
}
// property FormPrompt
public string FormPrompt
{
get { return formPrompt; }
set { formPrompt = value; }
}
// property InputResponse
public string InputResponse
{
get { return inputResponse; }
set { inputResponse = value; }
}
// property DefaultValue
public string DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; }
}
#endregion
#region Form and Control Events
/// <summary>
/// The InputBoxDialog form load event, sets focus to the
/// txtInput control
/// </summary>
/// <param name="sender">The InputBoxDialog</param>
/// <param name="e">The event arguments</param>
private void InputBox_Load(object sender, System.EventArgs e)
{
this.txtInput.Text = defaultValue;
this.lblPrompt.Text = formPrompt;
this.Text = formCaption;
this.txtInput.SelectionStart = 0;
this.txtInput.SelectionLength = this.txtInput.Text.Length;
this.txtInput.Focus();
}
/// <summary>
/// The btnOk click event, sets the InputResponse=txtInput
/// and then closes the form
/// </summary>
/// <param name="sender">The btnOK</param>
/// <param name="e">The event arguments</param>
private void btnOK_Click(object sender, System.EventArgs e)
{
InputResponse = this.txtInput.Text;
this.Close();
}
/// <summary>
/// The btnCancel click event, closes the form
/// </summary>
/// <param name="sender">The btnCancel</param>
/// <param name="e">The event arguments</param>
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
/// <summary>
/// The InputBoxDialog key down event, if the key == Enter, sets the
/// InputResponse=txtInput and then closes the form
/// </summary>
/// <param name="sender">The InputBoxDialog</param>
/// <param name="e">The event arguments</param>
private void InputBoxDialog_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
InputResponse = this.txtInput.Text;
this.Close();
}
}
#endregion
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog