Came across this method to validate a textbox for integers only:
As simpled as referencing the validateTextInteger Textbox textchanged event
private void validateTextInteger(object sender, EventArgs e)
{
Exception X = new Exception();
TextBox T = (TextBox)sender;
try
{
if (T.Text != “-”)
{
int x = int.Parse(T.Text);
}
}
catch (Exception)
{
try
{
int CursorIndex = T.SelectionStart – 1;
T.Text = T.Text.Remove(CursorIndex, 1);
MessageBox.Show(“Please use a valid integer”);
//Align Cursor to same index
T.SelectionStart = CursorIndex;
T.SelectionLength = 0;
}
catch (Exception) { }
}
}
