Here is an outline of what I want the program to do
So this is my sum method. I used a nested array loop for making the textboxes. I also put the displayTotal()
method.
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();
}
However, when I click the button the results won't display or even write a zero. Also, how do I make the sumOfBonus()
method more efficiently run?
Assuming your code and: statsBonus declared as TextBox[] and totalsBefore declared as TextBox[]
You don't really need the switch statement at all
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();
}
It's basically your own code with removed switch block.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.