yuurei
yuurei

Reputation: 11

Creating a Total of subtotals

Hi im trying to create this program where the txtbox1 will multiply on user's input and automatically shows the total in decimal point same as in other txtboxes, but i'm having a trouble to sum up all the subtotals of every result to overall txtbox total

here is my code in every subtotals

private void txtbox11_TextChanged_1(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtbox12.Text))
        txtTotal11.Text =
            (Convert.ToInt32(txtbox11.Text)*Convert.ToDecimal(112.61)).ToString();
}

private void txtbox12_TextChanged_1(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtbox12.Text))
        txtTotal12.Text =
            (Convert.ToInt32(txtbox12.Text) * Convert.ToDecimal(32.10)).ToString();
}

How do i automatically get the sum of txtTotal11 & txtTotal12 to display on txtTotal13? or should i put an event like "textChanged" on every txtTotal# too? thanks guys i'm really having a headache on this.

Upvotes: 1

Views: 276

Answers (2)

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35790

It will be something like:

private void txtboxSubTotal1_TextChanged_1(object sender, EventArgs e)
{
    CalcGrandTotal();
}

private void txtboxSubTotal2_TextChanged_1(object sender, EventArgs e)
{
    CalcGrandTotal();
}

private void CalcGrandTotal()
{
   decimal grandTotal = 0;
   decimal parseValue= 0;

    if (!string.IsNullOrEmpty(txtboxSubTotal1.Text) && decimal.TryParse(txtboxSubTotal1.Text, parseValue))
        grandTotal  += parseValue;

    if (!string.IsNullOrEmpty(txtboxSubTotal2.Text) && decimal.TryParse(txtboxSubTotal2.Text, parseValue))
        grandTotal  += parseValue;

txtboxGrandTotal.Text = grandTotal.ToString();
}

Upvotes: 1

Khadim Ali
Khadim Ali

Reputation: 2598

Try this: (in every TextChanged event)

decimal total13;
private void txtbox11_TextChanged_1(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtbox12.Text))
        var thisTotal = (Convert.ToInt32(txtbox11.Text)*Convert.ToDecimal(112.61)).ToString();

        txtTotal11.Text = thisTotal

        total13 += thisTotal
        txtTotal13.Text = thisTotal.ToString();
}

Upvotes: 0

Related Questions