tbf/DataStreamInterfaceTest/GetStateDlg.cs

86 lines
2.7 KiB
C#
Raw Permalink Normal View History

using System;
using System.Windows.Forms;
namespace DataStreamInterfaceTest
{
public partial class GetStateDlg : Form
{
/// <summary>
/// Integer number entered in this form
/// </summary>
public int State;
public string Parameter;
int lowerLimit;
int upperLimit;
/// <summary>
/// Default constructor
/// </summary>
public GetStateDlg()
: this("Enter state please")
{
}
/// <summary>
/// Constructor with a custom window title.
/// </summary>
/// <param name="title">Window title</param>
public GetStateDlg(string title)
: this(title, Int32.MinValue, Int32.MaxValue)
{
}
/// <summary>
/// Constructor with a custom window title, limits and non-empty initial value.
/// </summary>
/// <param name="title">Window title</param>
/// <param name="lowerLimit">Lower limit</param>
/// <param name="upperLimit">Upper limit</param>
/// <param name="initialValue">Initial value</param>
public GetStateDlg(string title, int lowerLimit, int upperLimit, int initialValue)
: this(title, lowerLimit, upperLimit)
{
stateTextBox.Text = initialValue.ToString();
}
/// <summary>
/// Constructor with a custom window title and lower/upper limits.
/// </summary>
/// <param name="title">Window title</param>
/// <param name="lowerLimit">Lower limit</param>
/// <param name="upperLimit">Upper limit</param>
public GetStateDlg(string title, int lowerLimit, int upperLimit)
{
InitializeComponent();
this.Text = title;
this.lowerLimit = lowerLimit;
this.upperLimit = upperLimit;
}
/// <summary>
/// OK button handler that verifies validity of the entered value.
/// </summary>
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
}
}
}
}