-1

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: Total Power Field issue

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();
    }
  • So you need to keep track of the current value, right? You already did this for `BillTotal`. Should be very similar for the total power used. – Klaus Gütter Jan 17 '23 at 06:32
  • 1
    And BTW: this question is in no way related to the development environment "Visual Studio", so you should not include this tag. OTOH, the tag "winforms" would be appropriate. – Klaus Gütter Jan 17 '23 at 06:34
  • Greetings @KlausGütter. No the BillTotal only shows the total for the previous entry. It updates with every new customer entry. I am building this in VS2019 (because 2022 was giving me issues with my validator, likely from a 2019 Solution last week) So I thought it relevant. Apologies if that was wrong. Is there a way for me to edit the tags once the question has been posed? – Arthwys Ironhand Jan 17 '23 at 07:01
  • 1
    "Is there a way for me to edit the tags" - yes, just click on the [edit] link below the question – Klaus Gütter Jan 17 '23 at 16:35

1 Answers1

0

If I understand your usecase correctly, you have multiple options to do this.

Or you add a property like your BillTotal for TotalPowerUsage.

You could also use linq to calculate the sum of the total usage:

var totalUsage = customers.Sum(x => x.PwrUse);

Or you add the value to the value already in the textbox:

var totalUsage = double.Parse(txtTtlPwr.Text) + PwrUse;

At the end you can show the totalUsage in your textbox.

txtTtlPwr.Text = totalUsage.ToString();
Presi
  • 806
  • 10
  • 26