im trying to create a loop to avoid copy pasting these lines 30 times.
the names is sum1 to sum30
br1txt1 to br30txt1
br1txt2 to br30txt2
// decimal sum30 = decimal.Parse(br30txt1.Text) + decimal.Parse(br30txt2.Text);
// sumTxt30.Text = sum30.ToString();
but the error im geting is that the textbox array seems to try to put the value of the textbox not the text box refrenc it self in to the array, how shold i fix this?
private void sumX()
{
TextBox[] sumTextboxNames;
sumTextboxNames = new TextBox[29];
for (int i = 1; i < 31; i++)
{
if (sumTextboxNames[0] == null)
{
int y = 0;
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Name.StartsWith("sum"))
{
sumTextboxNames[y] = (TextBox)c;
y++;
}
}
}
else
{
}
string1 = "br" + i + "txt" + 1 + ".Text";
string2 = "br" + i + "txt" + 2 + ".Text";
string3 = "sumTxt" + i + ".Text";
sum = decimal.Parse(string1) + decimal.Parse(string2);
int x = i - 1;
sumTextboxNames[x].Text = sum.ToString();
}
}
solution to this was this code.
private void sumX()
{
string string1;
string string2;
string string3;
for (int i = 1; i < 31; i++)
{
string1 = "br" + i + "txt" + '1';
string2 = "br" + i + "txt" + '2';
string3 = "sumTxt" + i;
TextBox s = (TextBox)this.Controls[string3];
TextBox b1 = (TextBox)this.Controls[string1];
TextBox b2 = (TextBox)this.Controls[string2];
decimal sum = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
s.Text = sum.ToString();
}