c# - 如何读回文本框并每次添加1

时间:2016-06-02 21:52:13

标签: c#

我正试图为我的朋友服装店制作服装销售图形用户界面。因此,当我输入价格和数量的商品时,它会显示总价,计算折扣并计算应付金额。然后,当我为下一个客户输入新价格和物品数量时,它会重复该过程。我的代码没有更新交易次数和总销售额($),只是重置它。我想返回之前的交易数量和总销售额,并将其添加到下一个计算中。请帮帮我们!

        private void btnCalculate_Click(object sender, EventArgs e)
    {
        double numberofitems;
        double price;
        double totalprice;
        double discount;
        double amountdue;
        double totalsales;
        double transactions = 0;



        // Total Price
        numberofitems = double.Parse(txtBoxNumberOfItems.Text);
        price = double.Parse(txtBoxPrice.Text);
        totalprice = numberofitems * price;
        txtBoxTotalPrice.Text = totalprice.ToString();

        // Discount
        discount = totalprice * 30 / 100;
        txtBoxDiscount.Text = discount.ToString();

        // Amount Due
        amountdue = totalprice - discount;
        txtBoxAmountDue.Text = amountdue.ToString();

        // Total Sales
        totalsales = amountdue;
        txtBoxTotalSales.Text = totalsales.ToString();


        // Number of Transactions
        transactions++;
        txtBoxTransactions.Text = transactions.ToString();

    }

        // Clear button
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBoxNumberOfItems.Text = "";
        txtBoxPrice.Text = "";
        txtBoxTotalPrice.Text = "";
        txtBoxDiscount.Text = "";
        txtBoxAmountDue.Text = "";
        txtBoxTotalSales.Text = "";
        txtBoxTransactions.Text = "";

    }
        // Exit Button
    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();

2 个答案:

答案 0 :(得分:0)

好的,如果我理解你的问题正确交易和总销售额重置,正如我之前提到的那样,这是因为你在btnCalculate_Click()方法中设置了这些变量。因此,默认情况下,每次单击“计算”按钮时,它们都会重置为0。这是由于范围可变,谷歌。要解决这个问题,您需要做的就是将transactionstotalsales作为类变量,而不是现在的本地方法变量。

// now class variables and can be seen by the whole class
double transactions = 0;
double totalsales;

private void btnCalculate_Click(object sender, EventArgs e)
{
    // method variables which can only be seen by this method
    double numberofitems;
    double price;
    double totalprice;
    double discount;
    double amountdue;

    // Total Price
    numberofitems = double.Parse(txtBoxNumberOfItems.Text);
    price = double.Parse(txtBoxPrice.Text);
    totalprice = numberofitems * price;
    txtBoxTotalPrice.Text = totalprice.ToString();

    // Discount
    discount = totalprice * 30 / 100;
    txtBoxDiscount.Text = discount.ToString();

    // Amount Due
    amountdue = totalprice - discount;
    txtBoxAmountDue.Text = amountdue.ToString();

    // Total Sales
    totalsales += amountdue;
    txtBoxTotalSales.Text = totalsales.ToString();

    // Number of Transactions
    transactions++;
    txtBoxTransactions.Text = transactions.ToString();
}

答案 1 :(得分:-1)

尝试这样的事情:

string objTextBox = t.Text;