I have a textbox on a form and a button on the same form. On the button click event I want to insist that the textbox has a value in the form of a Double. What I have so far is this –
public double getUnitStake(Form frontpage)
{
double doubleresult=0;
bool unitStake;
foreach (Control c in frontpage.Controls)
{
if (c.Name == "tbUnitStake")
{
unitStake = double.TryParse( (c as TextBox).Text, out doubleresult);
if (!unitStake)
{
}
else
{
doubleresult=double.Parse((c as TextBox).Text);
}
}
}
return doubleresult;
}
But for the life of me I can’t figure out what to do if the double.tryparse method is false. What I want is the program execution to halt until a suitable value has been entered into the textbox. How can I achieve that please? Thanks for all and any help.
It seems to me you are validating the contents of a textbox. Therefore you can use the validating event of a TextBox Control. Optionally connect an errorProvider control to it to give special focus on the error (blinking exclamation mark). The “e.Cancel” statement will prevent you from doing anything else with your form until a double is entered.