I am trying to get this piece of code working in C#, what I want to do is if a textfield is updated to validate its an integer (HP is an integer).
So by attempting to convert the input to an integer, I either get a new integer, and sets the value to it, or if it fails, it takes the previous verified value and pushes back to the text field (pretty much ignoring the input, updating it, and it is now a validated input).
In my head this is all logic and working, in effect its not. Please help.
private void Input_HP_TextChanged(object sender, EventArgs e)
{
Try
{
HP = Convert.ToInt32(Input_HP.Text);
}
catch(Exception e)
{
Input_HP.Text = HP;
}
}
You can use try.parse, but you will want to set it to a temporary variable. In your case, using HP will overwrite your variable you want to save.