JD2022-TU1/main/tools/framework/JD.MessageService/InputBox.cs

114 lines
No EOL
3.2 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace JD.MessageService
{
/// <summary>
/// Form used to retrieve some input from the user. Don't use directly.
/// Use <see cref="MessageService"/> instead.
/// </summary>
internal partial class InputBox : Form
{
#region Variables
private readonly string m_text;
private string m_result = string.Empty;
#endregion
#region Constructor
/// <summary>
/// Constructor
/// </summary>
/// <param name="text"></param>
/// <param name="caption"></param>
/// <param name="defaultValue"></param>
public InputBox(string text, string caption, string defaultValue)
{
InitializeComponent();
m_text = text;
LabelText = m_text;
Text = caption;
textBox.Text = defaultValue;
}
#endregion
#region Properties
/// <summary>
/// The text entered by the user.
/// </summary>
public string Result
{
get
{
return m_result;
}
}
private string LabelText
{
get
{
return label.Text;
}
set
{
Size size;
using (Graphics g = this.CreateGraphics())
{
Rectangle screen = Screen.PrimaryScreen.WorkingArea;
SizeF sizeF = g.MeasureString(value, label.Font, screen.Width - 20);
size = sizeF.ToSize();
size.Width += 4;
}
if (size.Width < 200)
size.Width = 200;
Size clientSize = this.ClientSize;
clientSize.Width += size.Width - label.Width;
clientSize.Height += size.Height - label.Height;
this.ClientSize = clientSize;
label.Text = value;
}
}
#endregion
#region Methods
/// <summary>
/// Shows the form as a modal dialog with the specified owner.
/// </summary>
/// <param name="owner">Any object that implements <see cref="IWin32Window"/> that represents the top-level window that will own this dialog.</param>
/// <param name="errorMessage">Error Message to display, null if none.</param>
/// <returns>One of the <see cref="DialogResult"/> values.</returns>
public DialogResult ShowDialog(IWin32Window owner, string errorMessage)
{
LabelText = string.IsNullOrEmpty(errorMessage) ? m_text : m_text + Environment.NewLine + errorMessage;
return base.ShowDialog(owner);
}
#endregion
#region Event Handlers
private void acceptButton_Click(object sender, EventArgs e)
{
m_result = textBox.Text;
DialogResult = DialogResult.OK;
}
private void cancelButton_Click(object sender, EventArgs e)
{
m_result = null;
DialogResult = DialogResult.Cancel;
}
#endregion
}
}