执行乘法并在文本框中显示结果

时间:2014-01-28 14:27:39

标签: c# winforms richtextbox multiplication

我有一个winform组合框,文本框......等等。 richTextBox1,textbox txtTotal和btnAdd。当我点击btnAdd几次我在richTextBox1中添加值时,txtTotal中的总数。我的问题是:如何在txtTotal中将它们相乘?

class Multy
{       
    public decimal Odds;
    public decimal Stake;
    public Multy()
    {}
}

private List<Multy> matches = new List<Multy>();

    private decimal transactionTotal;

    public decimal TransactionTotal
    {
        get { return transactionTotal; }
        set
        { 
            transactionTotal = value;
            txtTotal.Text = String.Format("{0:c}", transactionTotal);
        }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        matches = new List<Multy>();
        Multy match = new Multy();

        match.Odds = decimal.Parse(txtOdds.Text);
        match.Stake = decimal.Parse(txtStake.Text);
        matches.Add(match);

        decimal l_Odds;
        decimal l_Stake;

        string strOdds;
        string strStake;

        string strOutputLine;

        foreach (Multy m in matches)
        {               
            l_Odds = m.Odds;
            l_Stake = m.Stake;

            strOdds = l_Odds.ToString();
            strStake = l_Stake.ToString();

            strOutputLine = String.Format("{0}{1}{2} ",strOdds,strStake, Environment.NewLine);
            richTextBox1.AppendText(strOutputLine);

            TransactionTotal = transactionTotal + (decimal)m.Odds;
        }
    }

2 个答案:

答案 0 :(得分:0)

当您添加btnAdd时,如果您想保留它并进行乘法运算,那么您应该再设一个按钮btnMultiply来执行您的工作。

这很简单。只需将字符串转换为double,然后乘以(*)并在将其转换为String后将其显示为txtTotal

答案 1 :(得分:0)

您是否尝试过使用Decimal.Multiply方法?

var total = Decimal.Multiply(this.TransactionTotal, m.Odds);