using System;
using System.Windows.Forms;
namespace DataStreamInterfaceTest
{
public partial class GetStateDlg : Form
{
///
/// Integer number entered in this form
///
public int State;
public string Parameter;
int lowerLimit;
int upperLimit;
///
/// Default constructor
///
public GetStateDlg()
: this("Enter state please")
{
}
///
/// Constructor with a custom window title.
///
/// Window title
public GetStateDlg(string title)
: this(title, Int32.MinValue, Int32.MaxValue)
{
}
///
/// Constructor with a custom window title, limits and non-empty initial value.
///
/// Window title
/// Lower limit
/// Upper limit
/// Initial value
public GetStateDlg(string title, int lowerLimit, int upperLimit, int initialValue)
: this(title, lowerLimit, upperLimit)
{
stateTextBox.Text = initialValue.ToString();
}
///
/// Constructor with a custom window title and lower/upper limits.
///
/// Window title
/// Lower limit
/// Upper limit
public GetStateDlg(string title, int lowerLimit, int upperLimit)
{
InitializeComponent();
this.Text = title;
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
}
///
/// OK button handler that verifies validity of the entered value.
///
private void okButton_Click(object sender, EventArgs e)
{
int number;
if (int.TryParse(stateTextBox.Text, out number) && number >= lowerLimit && number <= upperLimit)
{
State = number;
Parameter = parameterTextBox.Text;
DialogResult = DialogResult.OK;
}
else
{
string message = (lowerLimit != Int32.MinValue || upperLimit != Int32.MaxValue)
? string.Format("Invalid state ({0}..{1})", lowerLimit, upperLimit)
: "Invalid state";
MessageBox.Show(message);
DialogResult = DialogResult.None; /// Prevent closing this window
}
}
}
}