Create InputBox for C# (2009-08-19)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace FujitsuTaskManger
{
///
/// The InputBox class is used to show a prompt in a dialog box using the static method Show().
///
///
/// Copyright ฉ 2003 Reflection IT
///
/// This software is provided 'as-is', without any express or implied warranty.
/// In no event will the authors be held liable for any damages arising from the
/// use of this software.
///
/// Permission is granted to anyone to use this software for any purpose,
/// including commercial applications, subject to the following restrictions:
///
/// 1. The origin of this software must not be misrepresented; you must not claim
/// that you wrote the original software.
///
/// 2. No substantial portion of the source code of this library may be redistributed
/// without the express written permission of the copyright holders, where
/// "substantial" is defined as enough code to be recognizably from this library.
///
///
public class InputBox : System.Windows.Forms.Form
{
protected System.Windows.Forms.Button buttonOK;
protected System.Windows.Forms.Button buttonCancel;
protected System.Windows.Forms.Label labelPrompt;
protected System.Windows.Forms.TextBox textBoxText;
protected System.Windows.Forms.ErrorProvider errorProviderText;
private IContainer components;
///
/// Delegate used to validate the object
///
private InputBoxValidatingHandler _validator;
private InputBox()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.textBoxText = new System.Windows.Forms.TextBox();
this.labelPrompt = new System.Windows.Forms.Label();
this.errorProviderText = new System.Windows.Forms.ErrorProvider(this.components);
((System.ComponentModel.ISupportInitialize)(this.errorProviderText)).BeginInit();
this.SuspendLayout();
//
// buttonOK
//
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.buttonOK.Location = new System.Drawing.Point(288, 72);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(75, 23);
this.buttonOK.TabIndex = 2;
this.buttonOK.Text = "OK";
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
//
// buttonCancel
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.CausesValidation = false;
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.Location = new System.Drawing.Point(376, 72);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 3;
this.buttonCancel.Text = "Cancel";
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// textBoxText
//
this.textBoxText.Location = new System.Drawing.Point(16, 32);
this.textBoxText.Name = "textBoxText";
this.textBoxText.Size = new System.Drawing.Size(416, 20);
this.textBoxText.TabIndex = 1;
this.textBoxText.Validating += new System.ComponentModel.CancelEventHandler(this.textBoxText_Validating);
this.textBoxText.TextChanged += new System.EventHandler(this.textBoxText_TextChanged);
//
// labelPrompt
//
this.labelPrompt.AutoSize = true;
this.labelPrompt.Location = new System.Drawing.Point(15, 15);
this.labelPrompt.Name = "labelPrompt";
this.labelPrompt.Size = new System.Drawing.Size(39, 13);
this.labelPrompt.TabIndex = 0;
this.labelPrompt.Text = "prompt";
//
// errorProviderText
//
this.errorProviderText.ContainerControl = this;
this.errorProviderText.DataMember = "";
//
// InputBox
//
this.AcceptButton = this.buttonOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.buttonCancel;
this.ClientSize = new System.Drawing.Size(464, 104);
this.Controls.Add(this.labelPrompt);
this.Controls.Add(this.textBoxText);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.Text = "Title";
//this.Load += new System.EventHandler(this.InputBox_Load);
((System.ComponentModel.ISupportInitialize)(this.errorProviderText)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void buttonCancel_Click(object sender, System.EventArgs e)
{
this.Validator = null;
this.Close();
}
private void buttonOK_Click(object sender, System.EventArgs e)
{
this.Close();
}
///
/// Displays a prompt in a dialog box, waits for the user to input text or click a button.
///
/// String expression displayed as the message in the dialog box
/// String expression displayed in the title bar of the dialog box
/// String expression displayed in the text box as the default response
/// Delegate used to validate the text
/// Numeric expression that specifies the distance of the left edge of the dialog box from the left edge of the screen.
/// Numeric expression that specifies the distance of the upper edge of the dialog box from the top of the screen
/// An InputBoxResult object with the Text and the OK property set to true when OK was clicked.
public static InputBoxResult Show(string prompt, string title, string defaultResponse, InputBoxValidatingHandler validator, int xpos, int ypos)
{
using (InputBox form = new InputBox())
{
form.labelPrompt.Text = prompt;
form.Text = title;
form.textBoxText.Text = defaultResponse;
if (xpos >= 0 && ypos >= 0)
{
form.StartPosition = FormStartPosition.Manual;
form.Left = xpos;
form.Top = ypos;
}
form.Validator = validator;
DialogResult result = form.ShowDialog();
InputBoxResult retval = new InputBoxResult();
if (result == DialogResult.OK)
{
retval.Text = form.textBoxText.Text;
retval.OK = true;
}
return retval;
}
}
///
/// Displays a prompt in a dialog box, waits for the user to input text or click a button.
///
/// String expression displayed as the message in the dialog box
/// String expression displayed in the title bar of the dialog box
/// String expression displayed in the text box as the default response
/// Delegate used to validate the text
/// An InputBoxResult object with the Text and the OK property set to true when OK was clicked.
public static InputBoxResult Show(string prompt, string title, string defaultText, InputBoxValidatingHandler validator)
{
return Show(prompt, title, defaultText, validator, -1, -1);
}
///
/// Reset the ErrorProvider
///
///
///
private void textBoxText_TextChanged(object sender, System.EventArgs e)
{
errorProviderText.SetError(textBoxText, "");
}
///
/// Validate the Text using the Validator
///
///
///
private void textBoxText_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Validator != null)
{
InputBoxValidatingArgs args = new InputBoxValidatingArgs();
args.Text = textBoxText.Text;
Validator(this, args);
if (args.Cancel)
{
e.Cancel = true;
errorProviderText.SetError(textBoxText, args.Message);
}
}
}
protected InputBoxValidatingHandler Validator
{
get
{
return (this._validator);
}
set
{
this._validator = value;
}
}
}
///
/// Class used to store the result of an InputBox.Show message.
///
public class InputBoxResult
{
public bool OK;
public string Text;
}
///
/// EventArgs used to Validate an InputBox
///
public class InputBoxValidatingArgs : EventArgs
{
public string Text;
public string Message;
public bool Cancel;
}
///
/// Delegate used to Validate an InputBox
///
public delegate void InputBoxValidatingHandler(object sender, InputBoxValidatingArgs e);
}
เวลาเรียกใช้ก็
using InputBoxSample;
แล้วก็
private void buttonTest_Click(object sender, System.EventArgs e) {
InputBoxResult result = InputBox.Show("Test input Box:", "AIam", "Default text", new InputBoxValidatingHandler(inputBox_Validating));
if (result.OK) {
textBox1.Text = result.Text;
}
}
private void inputBox_Validating(object sender, InputBoxValidatingArgs e) {
if (e.Text.Trim().Length == 0) {
e.Cancel = true;
e.Message = "Required";
}
}
ที่มา : http://www.bloggang.com/viewdiary.php?id=iaiam&month=08-2008&date=23&group=3&gblog=7
โค้ดผิดนิดหน่อยผมแก้ไขแล้ว object textBox และ Button เวลาจะ set drawing position ต้องระบุ property Location ให้มันด้วยครับ
|