c#按下按钮时将总数加在一起

时间:2017-02-21 01:53:29

标签: c# visual-studio

当我按下“buttonTotal”时,我需要它一起添加燃油价格。我有lblDailyPrice,lblTotalLitresEntered,我有3个按钮“无铅”; “柴油机”; “高级”......关于我如何实现这一目标的任何帮助?

private void btnTotal_Click(object sender, EventArgs e)
    {
        try
        {

            if(btnUnleaded_Click)
            {

            }
            else if (btnDiesel_Click)
            {

            }
            else if (btnPremium_Click)
            {

            }
        }
        catch
        {

        }
}

2 个答案:

答案 0 :(得分:0)

我建议制作“无铅”,“柴油”,“高级”选项RadioButtons而不是按钮,并将它们绑定在像Panel这样的容器下。因此,当用户按下“buttonTotal”时,程序将检查哪个RadioButton被检查,然后计算相应的值。

private void btnTotal_Click(object sender, EventArgs e)
{
        double totalPrice = 0;
        if(unleadedRadioButton.Checked)
        {
             totalPrice = lblDailyPrice * lblTotalLitresEntered * unleadedValue;
        }
        else if (dieselRadioButton.Checked)
        {
             totalPrice = lblDailyPrice * lblTotalLitresEntered * dieselValue;
        }
        else if (premiumRadioButton.Checked)
        {
             totalPrice = lblDailyPrice * lblTotalLitresEntered * premiumValue;
        }
}

答案 1 :(得分:-3)

我假设你使用的是Windows Forms。我已经链接了有关如何完成此任务的相关文档。只需一点点阅读,就应该很容易。祝你好运,欢迎来到StackOverflow!。

  1. Get the prices from the labels.
  2. Convert those strings into a number type with a decimal.
  3. 然后只对结果变量进行数学运算。

    示例:

    var total = Convert.ToDecimal(lblDailyPrice.Text) * Convert.ToDecimal(lblTotalLitresEntered.Text);