I would like my application to to check if all 5 textboxes are numeric, and if the values are all true. Display the Talley. If not, I don't want my method to execute and right now it is. The way I have IsValid method coded, to me seems like it should work. I guess if someone could point me in the right direction, I'm sure there is a simple way of doing something such as this but I haven't found it. Thanks in advance to anyway that takes their time to look at this.
I have tried several variations of the example here, but haven't been able to do what it needs to do.
private void btnCalculate_Click(object sender, EventArgs e)
{
if (IsValid()) // if everything passes it will hit this
{
double total;
double hospitalCharge;
hospitalCharge = CalcStayCharges() * Convert.ToDouble(txtDays.Text);
total = hospitalCharge + CalcMiscCharges();
CalcTotalCharges(total);
lblDisplay.Text = "The total is " + CalcTotalCharges(total).ToString("c");
}
//else return;
// IsNumber(txtDays.Text.ToString()); // testing somehing
}
private double CalcStayCharges()
{
double hosipalCharge = 350;
return hosipalCharge;
}
private double CalcMiscCharges()
{
double Totalcharges;
Totalcharges = Convert.ToDouble(txtLab.Text) + Convert.ToDouble(txtMedication.Text) + Convert.ToDouble(txtRehab.Text) + Convert.ToDouble(txtSurgical.Text);
return Totalcharges;
}
private double CalcTotalCharges(double total)
{
return total;
}
private bool IsNumber(TextBox myNumber)
{
if (double.TryParse(Convert.ToString(myNumber), out double n))
{
// MessageBox.Show("string");
return false;
}
// MessageBox.Show("number"); // this is how you can test a bool value
return true;
}
private bool IsValid()
{
bool validation = false;
foreach (Control ctl in this.Controls)
{
if (ctl is TextBox)
{
TextBox tb = ctl as TextBox;
IsNumber(tb);
if (IsNumber(tb) == true)
{
validation = true;
}
else if (IsNumber(tb) == false)
{
validation = false;
}
}
}
return validation;
}