簡體   English   中英

如何匯總和顯示文本框每一列的結果?

[英]How do I sum and display the result of each column of textboxes?

這是我要程序執行的概述

這就是我想要的結果看起來像總成績

這就是我的求和方法。 我使用了嵌套數組循環來制作文本框。 我還放了displayTotal()方法。

 private void sumOfBonus()
    {
        bonusAttack = 0;
        bonusMain = 0;
        bonusSecondary = 0;

        attackPercent = 0;
        mainPercent = 0;
        secondaryPercent = 0;

        for (int j = 0; j < statsBonus.GetLength(0); j++)
        {
            switch (statsBonus.GetLength(0))
            {
                case 0:
                    for (int i = 0; i < statsBonus.GetLength(1); i++)
                    {
                        bonusAttack += Convert.ToInt32(statsBonus[j, i]);
                    }

                    totalsBefore[j].Text = bonusAttack.ToString();
                    break;

                case 1:
                    for (int i = 0; i < statsBonus.GetLength(1); i++)
                    {
                        bonusMain += Convert.ToInt32(statsBonus[j, i]);
                    }

                    totalsBefore[j].Text = bonusMain.ToString();
                    break;

                case 2:
                    for (int i = 0; i < statsBonus.GetLength(1); i++)
                    {
                        bonusSecondary += Convert.ToInt32(statsBonus[j, i]);
                    }

                    totalsBefore[j].Text = bonusSecondary.ToString();
                    break;

                case 3:
                    for (int i = 0; i < statsBonus.GetLength(1); i++)
                    {
                        attackPercent += Convert.ToInt32(statsBonus[j, i]);
                    }

                    totalsBefore[j].Text = attackPercent.ToString();
                    break;

                case 4:
                    for (int i = 0; i < statsBonus.GetLength(1); i++)
                    {
                        mainPercent += Convert.ToInt32(statsBonus[j, i]);
                    }

                    totalsBefore[j].Text = mainPercent.ToString();
                    break;

                case 5:
                    for (int i = 0; i < statsBonus.GetLength(1); i++)
                    {
                        secondaryPercent += Convert.ToInt32(statsBonus[j, i]);
                    }

                    totalsBefore[j].Text = secondaryPercent.ToString();
                    break;
            }
        }
    }

    private void totalsBoxes()
    {
    // this was called in the initializer method
        for (int i = 0; i < totalsBefore.Length; i++)
        {
            totalsBefore[i] = new System.Windows.Forms.TextBox();
            this.totalsBefore[i].Location = new System.Drawing.Point(190 + 60*i, 60);
            this.totalsBefore[i].Name = "totalBefore" + statsLabel[i];
            this.totalsBefore[i].Size = new System.Drawing.Size(50, 20);
            this.totalsBefore[i].TabIndex = i + 1;
            this.totalsBefore[i].ReadOnly = true;
            totalsLabel[i] = new System.Windows.Forms.Label();
            this.totalsLabel[i].Location = new System.Drawing.Point(190 + 60 * i, 36);
            this.totalsLabel[i].Name = "totalLabel" + i;
            this.totalsLabel[i].Size = new System.Drawing.Size(55, 20);
            this.totalsLabel[i].AutoSize = true;
            this.totalsLabel[i].TabIndex = i + 1;
            this.totalsLabel[i].Text = statsLabel[i];
            totalsBefore[i].Parent = this;
            this.groupBox3.Controls.Add(totalsLabel[i]);
            this.groupBox3.Controls.Add(totalsBefore[i]);
        }
    }

private void button1_Click(object sender, EventArgs e)
{
    sumofBonus();
}

但是,當我單擊按鈕時,結果將不會顯示,甚至不會寫零。 另外,如何使sumOfBonus()方法更有效地運行?

假設您的代碼和:statsBonus聲明為TextBox []和totalsBefore聲明為TextBox []

您真的根本不需要switch語句

for (int j = 0; j < statsBonus.GetLength(0); j++)
{
    int sum = 0;
    for (int i = 0; i < statsBonus.GetLength(1); i++)
    {
        sum+= Int32.Parse(statsBonus[j,i].Text);
    }
    totalsBefore[j].Text = sum.ToString();
}

基本上,這是您自己的代碼,其中刪除了開關塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM