When trying a regular expression for no characters my winforms EnterValue is still being triggered after the if statement, how can I stop it from going any further after the trigger?
private void EnterValue_Click(object sender, EventArgs e)
{
if (textBox1.Text != string.Empty && !Regex.IsMatch(textBox1.Text, @"^[0-9]+$"))
{
MessageBox.Show("Please only enter numbers");
textBox1.Clear();
}
//convert input to double
listDouble.Add(Convert.ToDouble(textBox1.Text)); // this line still throws exception
textBox1.Clear();
//clear existing items
listBox1.Items.Clear();
// clear any existing list items
for (int i = 0; i < listDouble.Count; i++)
{
listBox1.Items.Add(listDouble[i]);
}
//for each value added, add this to our list
}
Return from the method:
This will execute only if the
ifpredicate is true, and the method will return as soon as thereturn;statement has been hit, without any of the other code being run.