1 Name: Anonymous : 2021-04-21 04:52 ID:ku6ArAEx [Del]
private bool IsValidInteger (out int result, TextBox txt, string name, int min = int.MinValue, int max = int.MaxValue)
{
// Give result an initial value.
result = int.MinValue;
// Get the text.
string text = txt.Text;
// If there is something wrong, build an error message.
string message = "";
if (text.Length == 0) message = "Please enter" + name + ".";
else if (!int.TryParse(text, out result))
message = "Error parsing" + name + "'" + text + "'";
else if ((result max))
message = name + " must be between " + min.ToString() + " and " + max.ToString() + ".";
// See if we have an error message.
if (message.Length > 0)
{
//Display the message, select the TextBox's text,
// give it focus, and return false.
MessageBox.Show(message, name + "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txt.Select(0, text.Length);
txt.Focus();
return false;
}
// The value is Okay.
return true;
}