Struggling with a C# windows form app. I've got the form built and the basic functions (can enter the values in the form fields) and they populate a listbox properly (yay!), and the textboxes clear for the next entry. This is a version of the good old Electrical Bill assignment.
I need to code a textbox to sum the total power usage reported by all the customer entries as we go along, and I'm not sure how to go about it.
I'm fairly new to C# and this one is making my head hurt. I imagine it's something simple, but I'm just not finding the way.
Here's the Form: with the textbox in question highlighted:
So when the customer enters their data and hits the Add Data button the input data gets displayed in the list box and the data fields clear for the next use.
The Bill Total, and Total Power Used text boxes(ReadOnly) display their relevant data, But when the next Customer enters their information, the Total Power Used field replaces the previous data with the new power usage. I need it to keep the previous data and add the new input to the total.
This is what I've got for the form code atm: (apologies if I messed up the formatting again.)
public partial class ElectricBill : Form
{
List<Customers> customers = new List<Customers>();// empty
decimal BillTotal = 0;
public ElectricBill()
{
InitializeComponent();
}
// add customer data
private void btnAddData_Click(object sender, EventArgs e)
{
string FirstName;
string LastName;
decimal AcctNo, PwrUse;
Customers Bill;
if (Validator.IsProvided(txtFname) &&
Validator.IsProvided(txtLname) &&
Validator.IsNonNegativeDecimal(txtAcctNo) &&
Validator.IsNonNegativeDecimal(txtPwrUse)
) // valid data provided
{
// get the data
FirstName = txtFname.Text;
LastName = txtLname.Text;
AcctNo = Convert.ToDecimal(txtAcctNo.Text);
PwrUse = Convert.ToDecimal(txtPwrUse.Text);
// create new Customer Bill entry
Bill = new Customers(FirstName, LastName, (int)AcctNo, PwrUse);
}
else // invalid data provided
{
return;
}
// process the provided data
customers.Add(Bill);
lstCustomerData.Items.Add(Bill);
BillTotal += Bill.CalculateBill();
txtBillTotal.Text = BillTotal.ToString("c");
txtTtlPwr.Text = txtPwrUse.Text.ToString();
ResetEntry();
} // end add
// prepare form for next customer entry
private void ResetEntry()
{
txtFname.Clear();
txtLname.Clear();
txtAcctNo.Clear();
txtPwrUse.Clear();
txtFname.Focus();
}
private void btnReset_Click(object sender, EventArgs e)
{
txtFname.Clear();
txtLname.Clear();
txtAcctNo.Clear();
txtPwrUse.Clear();
}