Method 1: RegEx
You should try to use a regular expression. Regular expressions (Regex in short) are used to match strings against patterns. For example, if you only want to allow integers:
Regex r = new Regex(@"^[0-9]+$")
The Regex
class has an .IsMatch(string s)
method, where s
is the string you want to test against the pattern.
Method 2: try-catch and Parse()
Another way to do it, which might be a bit more beginner-friendly, is a try-catch block. (I am assuming your TextBox's name is TextBox1 and you store the sum value in a runningSum variable.)
try {
double x = double.Parse(TextBox1.Text);
runningSum += x;
catch (ArgumentException ax) {
//handle if it is not a number in the TextBox
}
Method 3: TryParse()
A more advanced version which combines try-catch
and Parse()
is using the double.TryParse()
method which returns a true/false value based on whether conversion was successful or not.
double x;
if (double.TryParse(TextBox1.Text, out x)) {
runningSum += x;
} else {
//handle if it is not a number in the TextBox.
}