📄 inputbox.cs
字号:
////////////////////////////////////////////////
//
// Project: Lines.NET
// Version: 1.1
// Author: Vladimir L.
//
// homepage: http://www.boomsoft.org
// e-mail: support@boomsoft.org
//
// Copyright (c) 2003-2004, Boomsoft.org
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Lines
{
/// <summary>
/// Defines a dialog to inquire a user for text input.
/// </summary>
/// <remarks>
/// The dialog places itself automatically to the center of screen.
/// </remarks>
/// <example>
/// <code>
/// // Create an instance of InputBox dialog.
/// InputBox dialog = new InputBox();
///
/// // Set a dialog title.
/// dialog.Text = "Dialog title";
///
/// // Set a dialog message.
/// dialog.Message = "Please, enter your name:";
///
/// // Do not display cancel button.
/// dialog.DisplayCancelButton = false;
///
/// // Show dialog.
/// dialog.ShowDialog();
///
/// // Use the results of user input.
/// System.Console.WriteLine(dialog.InputText);
/// </code>
/// </example>
public class InputBox : Form
{
private Button buttonOK;
private Button buttonCancel;
private Label labelMessage;
private TextBox textInput;
/// <summary>
/// A trick
/// </summary>
private MainMenu menuStub;
/// <summary>
/// Defines whether the button OK was pressed or not.
/// </summary>
private bool isOk = false;
/// <summary>
/// Gets or sets the message that will be displayed to describe an inquiry.
/// </summary>
public string Message
{
get {return labelMessage.Text;}
set {labelMessage.Text = value;}
}
/// <summary>
/// Gets or sets the text of input field.
/// </summary>
/// <remarks>
/// Use this property to retrieve the user input after the button OK was pressed. Use
/// <see cref="IsOk"/> property to determine if the user has pressed the OK button.
/// </remarks>
public string InputText
{
get {return textInput.Text;}
set {textInput.Text = value;}
}
/// <summary>
/// Gets or sets a flag whether display Cancel button on dialog or not.
/// </summary>
/// <remarks>
/// If this property set to <c>false</c> the Cancel button will be hidden away from dialog.
/// </remarks>
public bool DisplayCancelButton
{
get {return buttonCancel.Visible;}
set {buttonCancel.Visible = value;}
}
/// <summary>
/// Gets the value of <see cref="isOk"/> property.
/// </summary>
/// <remarks>
/// This property is set to <c>true</c> if OK button was pressed while closing this dialog,
/// otherwise this property remain the value <c>false</c>.
/// </remarks>
public bool IsOk
{
get {return isOk;}
}
/// <summary>
/// Creates an instance of InputBox class.
/// </summary>
/// <param name="isPocketPC">Defines whether the dialog is used under Pocket PC 2002/2003 version
/// of .NET Compact Framework, or under Windows CE.NET 4.0/4.1</param>
public InputBox(bool isPocketPC)
{
// Required for Windows Form Designer support
InitializeComponent();
// A trick to display SIP (soft input panel) if the dialog used under Pocket PC version.
// :( There is no other way to get SIP in .NET Compact Framework
// And SIP is not needed for Windows CE.NET 4.0/4.1
if (isPocketPC)
{
menuStub = new MainMenu();
Menu = menuStub;
}
// Place dialog at the center of screen
Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
this.Left = (workingArea.Width - this.Width) / 2;
this.Top = (workingArea.Height - this.Height) / 2;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
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.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.labelMessage = new System.Windows.Forms.Label();
this.textInput = new System.Windows.Forms.TextBox();
//
// buttonOK
//
this.buttonOK.Location = new System.Drawing.Point(160, 8);
this.buttonOK.Size = new System.Drawing.Size(56, 24);
this.buttonOK.Text = "OK";
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
//
// buttonCancel
//
this.buttonCancel.Location = new System.Drawing.Point(160, 32);
this.buttonCancel.Size = new System.Drawing.Size(56, 24);
this.buttonCancel.Text = "Cancel";
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// labelMessage
//
this.labelMessage.Location = new System.Drawing.Point(8, 8);
this.labelMessage.Size = new System.Drawing.Size(152, 48);
this.labelMessage.Text = "Message";
//
// textInput
//
this.textInput.Location = new System.Drawing.Point(8, 64);
this.textInput.Size = new System.Drawing.Size(208, 22);
this.textInput.Text = "";
//
// InputBox
//
this.ClientSize = new System.Drawing.Size(226, 96);
this.Controls.Add(this.textInput);
this.Controls.Add(this.labelMessage);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "InputForm";
this.Load += new System.EventHandler(this.InputBox_Load);
}
#endregion
/// <summary>
/// Sets the flag <see cref="IsOk"/> to <c>true</c> and closes dialog.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonOK_Click(object sender, System.EventArgs e)
{
isOk = true;
Close();
}
/// <summary>
/// Sets the flag <see cref="IsOk"/> to <c>false</c> and closes dialog.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCancel_Click(object sender, System.EventArgs e)
{
isOk = false;
Close();
}
/// <summary>
/// Sets the focus to input field.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InputBox_Load(object sender, System.EventArgs e)
{
textInput.Focus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -