创建总计小计

时间:2015-01-14 07:09:05

标签: c# textbox

你好我试图创建这个程序,其中txtbox1将乘以用户的输入并自动显示与其他txtboxes相同的小数点总数,但我很难总结每个结果的所有小计到总体txtbox总计

这是我在每个小计中的代码

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();
}

如何自动获取txtTotal11&的总和? txtTotal12在txtTotal13上显示? 或者我应该在每个txtTotal#上放置像“textChanged”这样的事件吗? 谢谢你们,我真的很头疼。

2 个答案:

答案 0 :(得分:1)

这将是:

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();
}

答案 1 :(得分:0)

试试这个:(在每个TextChanged事件中)

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();
}