using System;
using System.Drawing;
using System.Windows.Forms;
namespace JD.MessageService
{
///
/// Form used to retrieve some input from the user. Don't use directly.
/// Use instead.
///
internal partial class InputBox : Form
{
#region Variables
private readonly string m_text;
private string m_result = string.Empty;
#endregion
#region Constructor
///
/// Constructor
///
///
///
///
public InputBox(string text, string caption, string defaultValue)
{
InitializeComponent();
m_text = text;
LabelText = m_text;
Text = caption;
textBox.Text = defaultValue;
}
#endregion
#region Properties
///
/// The text entered by the user.
///
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
///
/// Shows the form as a modal dialog with the specified owner.
///
/// Any object that implements that represents the top-level window that will own this dialog.
/// Error Message to display, null if none.
/// One of the values.
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
}
}