private void textBoxDependents_Leave(object sender, EventArgs e) { // make sure data is numeric int parseResult = 0; int maxDependents = PaycheckCalculation.getMaxDependents(); // make sure data is numeric if (int.TryParse(textBoxDependents.Text, out parseResult) == false) { // if can't parse parseResult = 0; // reset to zero } parseResult = parseResult > maxDependents ? maxDependents : parseResult; // don't surpass max dependents textBoxDependents.Text = parseResult.ToString("0"); // format correctly }
private void buttonCalculate_Click(object sender, EventArgs e) { /* error handling */ /* After adding input correction, this error handling never really gets used */ bool success = true; // whether there were any errors List <string> errorFields = new List <string>(); // list of fields which have an error List <Label> fieldLabels = new List <Label> { labelGross, labelDependents, labelMaritalStatus, labelPayPeriod }; // input labels /* init input variables */ PaycheckCalculation calculation; string name = ""; double grossPay = 0.0; int dependents = 0; PaycheckCalculation.maritalStatusOptions maritalStatus; PaycheckCalculation.payPeriodOptions payPeriod; /* results */ double[] resultData; /* clear errors */ foreach (Label lbl in fieldLabels) { lbl.clearError(); } /* gather input */ name = ""; // not used in this version? if (double.TryParse(textBoxGrossPay.Text, out grossPay) == false) // try to parse gross pay { success = labelGross.showError(ref errorFields); } if (int.TryParse(textBoxDependents.Text, out dependents) == false) // try to parse dependents { success = labelDependents.showError(ref errorFields); } else // check if max dependents has been reached { dependents = dependents > 10 ? 10 : dependents; } if (Enum.TryParse(comboBoxMaritalStatus.Text, true /* ignore case */, out maritalStatus) == false) // try to parse marital status { success = labelMaritalStatus.showError(ref errorFields); } if (Enum.TryParse(comboBoxPayPeriod.Text, true /* ignore case */, out payPeriod) == false)// try to parse pay period { success = labelPayPeriod.showError(ref errorFields); } /* success or failure */ if (success) { calculation = new PaycheckCalculation(name, grossPay, dependents, maritalStatus, payPeriod); // pass input to constructor calculation.calculate(); // perform calculations resultData = calculation.getResults(); // extract results for output // display the results textBoxResultGross.Text = resultData[0].ToString("0.00"); textBoxResultFederal.Text = resultData[1].ToString("0.00"); textBoxSocial.Text = resultData[2].ToString("0.00"); textBoxResultMedicare.Text = resultData[3].ToString("0.00"); textBoxResultNet.Text = resultData[4].ToString("0.00"); } else // there was some issue with an input { // this scenario never errors } }