public partial class InputBox
: Form
{
public String Input
{
get { return textInput.Text; }
}
public InputBox()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
private void InputBox_Load(object sender, EventArgs e)
{
this.ActiveControl = textInput;
}
public static DialogResult Show(String title, String message, String inputTitle, out String inputValue)
{
InputBox inputBox = null;
DialogResult results = DialogResult.None;
using (inputBox = new InputBox() { Text = title })
{
inputBox.labelMessage.Text = message;
inputBox.splitContainer2.SplitterDistance = inputBox.labelMessage.Width;
inputBox.labelInput.Text = inputTitle;
inputBox.splitContainer1.SplitterDistance = inputBox.labelInput.Width;
inputBox.Size = new Size(
inputBox.Width,
8 + inputBox.labelMessage.Height + inputBox.splitContainer2.SplitterWidth + inputBox.splitContainer1.Height + 8 + inputBox.button2.Height + 12 + (50));
results = inputBox.ShowDialog();
inputValue = inputBox.Input;
}
return results;
}
void labelInput_TextChanged(object sender, System.EventArgs e)
{
}
}
partial class InputBox
{
/// <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.labelMessage = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.labelInput = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Location = new System.Drawing.Point(3, 0);
this.labelMessage.MaximumSize = new System.Drawing.Size(379, 0);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(50, 13);
this.labelMessage.TabIndex = 99;
this.labelMessage.Text = "Message";
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(316, 126);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Cancel";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(235, 126);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "OK";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// labelInput
//
this.labelInput.AutoSize = true;
this.labelInput.Location = new System.Drawing.Point(3, 6);
this.labelInput.Name = "labelInput";
this.labelInput.Size = new System.Drawing.Size(31, 13);
this.labelInput.TabIndex = 99;
this.labelInput.Text = "Input";
this.labelInput.TextChanged += new System.EventHandler(this.labelInput_TextChanged);
//
// textInput
//
this.textInput.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textInput.Location = new System.Drawing.Point(3, 3);
this.textInput.Name = "textInput";
this.textInput.Size = new System.Drawing.Size(243, 20);
this.textInput.TabIndex = 1;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
this.splitContainer1.IsSplitterFixed = true;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.labelInput);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textInput);
this.splitContainer1.Size = new System.Drawing.Size(379, 50);
this.splitContainer1.SplitterDistance = 126;
this.splitContainer1.TabIndex = 99;
//
// splitContainer2
//
this.splitContainer2.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.splitContainer2.IsSplitterFixed = true;
this.splitContainer2.Location = new System.Drawing.Point(12, 12);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.labelMessage);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.splitContainer1);
this.splitContainer2.Size = new System.Drawing.Size(379, 108);
this.splitContainer2.SplitterDistance = 54;
this.splitContainer2.TabIndex = 99;
//
// InputBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(403, 161);
this.Controls.Add(this.splitContainer2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label labelMessage;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label labelInput;
private System.Windows.Forms.TextBox textInput;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
}
用法:
String output = "";
result = System.Windows.Forms.DialogResult.None;
result = InputBox.Show(
"Input Required",
"Please enter the value (if available) below.",
"Value",
out output);
if (result != System.Windows.Forms.DialogResult.OK)
{
return;
}