private void btnCalculate_Click(object sender, EventArgs e) { const decimal U_CUST = 999; const decimal E_CUST = 249; const decimal ADD_E_CUST = 99; const decimal H_CUST = 99; const decimal ADD_H_CUST = 49; char CustType; int AdditionalPrograms = 0; int Installations = 10; decimal Bill = 0; if (txtCustType.Text.Length != 1) { MessageBox.Show("Customer type is required.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); txtCustType.Focus(); return; } CustType = char.Parse(txtCustType.Text); CustType = char.ToUpper(CustType); txtCustType.Text = CustType.ToString(); if (CustType != 'U' && CustType != 'H' && CustType != 'E') { MessageBox.Show("Invalid customer type - must be H, E or U.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); txtCustType.Focus(); return; } //calculate bill according to customer type if (CustType == 'U') { Bill = U_CUST; } else { if (txtAddPrograms.Text != "") // AdditionalPrograms will remain at 0 if textbox is empty { bool isValid = int.TryParse(txtAddPrograms.Text, out AdditionalPrograms); if (!isValid || AdditionalPrograms < 0) { MessageBox.Show("Invalid number of additional programs - can't be negative.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); txtAddPrograms.Focus(); return; } } if (CustType == 'H') { Bill = H_CUST + ADD_H_CUST * AdditionalPrograms; } else // Customer Type must be 'E' { if (txtInstallations.Text != "") // Installations will remain at 10 if textbox is empty { bool isValid = int.TryParse(txtInstallations.Text, out Installations); if (!isValid || Installations < 1) { MessageBox.Show("Invalid number of installations - must be positive.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); txtInstallations.Focus(); return; } } Bill = E_CUST + ADD_E_CUST * AdditionalPrograms; if (Installations > 10) Bill *= 1 + (Installations - 10) * 0.1m; } } //Display the result. lblBill.Text = Bill.ToString("C"); if (CustType == 'E' && Installations > 40) { MessageBox.Show("Note that the unlimited plan would be more economic.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } }