ShareX/HelpersLib/Forms/InputBox.cs
2014-01-12 09:25:51 +02:00

175 lines
6.1 KiB
C#

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2014 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Windows.Forms;
namespace HelpersLib
{
public class InputBox : Form
{
public string Question { get; set; }
public string InputText { get; set; }
public InputBox()
{
InitializeComponent();
}
public InputBox(string question, string inputText)
{
Question = question;
InputText = inputText;
}
private void InputBox_Load(object sender, EventArgs e)
{
lblQuestion.Text = Question ?? "";
txtInputText.Text = InputText ?? "";
}
private void InputBox_Shown(object sender, EventArgs e)
{
BringToFront();
txtInputText.Focus();
txtInputText.SelectionLength = txtInputText.Text.Length;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtInputText.Text))
{
InputText = txtInputText.Text;
DialogResult = DialogResult.OK;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
public static string GetInputText(string question = "", string inputText = "")
{
using (InputBox form = new InputBox(question, inputText))
{
if (form.ShowDialog() == DialogResult.OK)
{
return form.InputText;
}
return null;
}
}
#region Windows Form Designer generated code
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.txtInputText = new System.Windows.Forms.TextBox();
this.lblQuestion = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(208, 56);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 24);
this.btnOK.TabIndex = 2;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(288, 56);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(72, 24);
this.btnCancel.TabIndex = 3;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// txtInputText
//
this.txtInputText.Location = new System.Drawing.Point(8, 32);
this.txtInputText.Name = "txtInputText";
this.txtInputText.Size = new System.Drawing.Size(352, 20);
this.txtInputText.TabIndex = 1;
//
// lblQuestion
//
this.lblQuestion.AutoSize = true;
this.lblQuestion.Location = new System.Drawing.Point(8, 8);
this.lblQuestion.Name = "lblQuestion";
this.lblQuestion.Size = new System.Drawing.Size(31, 13);
this.lblQuestion.TabIndex = 0;
this.lblQuestion.Text = "Input";
//
// InputBox
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(369, 89);
this.Controls.Add(this.lblQuestion);
this.Controls.Add(this.txtInputText);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "InputBox";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "InputBox";
this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.Shown += new System.EventHandler(this.InputBox_Shown);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label lblQuestion;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.TextBox txtInputText;
#endregion Windows Form Designer generated code
}
}