无法获得基本价格以输出到小计

时间:2018-12-10 03:29:53

标签: c# winforms calculator

我正在尝试创建一种计算器,将基础价格加起来,添加任何单选按钮/复选标记,再加上税金,然后将结果汇总到小计中。为什么我无法将基本价格文本框显示在小计文本框中?香港专业教育学院尝试了几种方法,但它们似乎没有用。有什么建议吗? 这是我的代码:

 public partial class Form1 : Form

{
    public Form1()
    {
        InitializeComponent();   
    }
            private void txttradein_TextChanged(object sender, EventArgs e)
    {
        lblTradein.Text = txtTradeIn.Text;
    }
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        double BasePrice = double.Parse(txtBasePrice.Text);
        try
        {
          if (BasePrice < 0)
            {
                MessageBox.Show("Error");
                txtBasePrice.Focus();
                txtBasePrice.SelectAll();
            }
        }
        catch (FormatException)
        {
            txtBasePrice.SelectAll();
            MessageBox.Show("Numbers Only");

        }
        try
        {
            double TradeIn= double.Parse(txtTradeIn.Text);
            if(TradeIn < 0)
            {
                MessageBox.Show("Error");
                txtTradeIn.Focus();
                txtTradeIn.SelectAll();
            }
        }
        catch (FormatException)
        {
            txtTradeIn.SelectAll();
            MessageBox.Show("Numbers Only");

        }
        double stereoPrice = 0, leatherPrice = 0, navPrice = 0;
        if (chkStereo.Checked) {
            stereoPrice = 425.76;
        } if (chkLeather.Checked){ 
                leatherPrice = 987.41;
        } if (chkComputer.Checked){ 
                navPrice = 1741.23;
        }

        double exteriorFinish = 0;
        if(radPearlized.Checked)
        {
            exteriorFinish = 345.72;

        }
        else if (radCustom.Checked)
        {
            exteriorFinish = 599.99;
        }
        else
        {
            exteriorFinish = 0;
        }

        double subTotal = stereoPrice + leatherPrice + navPrice +   exteriorFinish;

        double txtRate = 0.08, tax = 0;
        tax = subTotal * txtRate;
        txtTax.Text = tax.ToString();
        txtSubtotal.Text = subTotal.ToString();
    }
  }

}

1 个答案:

答案 0 :(得分:1)

请在try块外声明基本价格变量。此变量在try块内部声明,因此无法在外部访问。

此外,您甚至没有在计算中使用基准价格变量。见下文-

    double subTotal = stereoPrice + leatherPrice + navPrice +   exteriorFinish;