Darkmage
Darkmage

Reputation: 1603

Puting textboxes into a TextBox[] array

I'm trying to create a loop to avoid copy pasting these lines 30 times.

The names are:

//decimal sum30 = decimal.Parse(br30txt1.Text) + decimal.Parse(br30txt2.Text);
//sumTxt30.Text = sum30.ToString();  

But the error I'm getting 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 should 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();  
        }  
   }

Upvotes: 0

Views: 5508

Answers (3)

Darkmage
Darkmage

Reputation: 1603

Thanks to Thorsten this is what I ended up with:

string string1;
string string2;
string string3;

private void sumX()
{
    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();
    }

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

The following lines won't work at all:

string1 = "br" + i + "txt" + 1 + ".Text";
string2 = "br" + i + "txt" + 2 + ".Text";

As 1 and 2 are not strings and can not be concatenated to a string. That should give a compiler error right away.

In the following line, you're trying to add up the names of the text boxes as numbers - won't work, the names contain non-number characters.

sum = decimal.Parse(string1) + decimal.Parse(string2);

Anyway, you don't need to use the TextBox array at all. What you could do is:

for (int i = 1; i <= 30; i++)
{

    TextBox s = (TextBox)this.Controls[<Name for the S-Textbox>];
    TextBox b1 = (TextBox)this.Controls[<Name for the first sum textbox>];
    TextBox b2 = (TextBox)this.Controls[<Name for the second sum textbox>];

    s.Text = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
}

EDIT
Sorry, quoted wrong line from OP's source code.

EDIT 2
Forgot to cast to TextBox - this is required of course... Thanks for pointing it out, everybody.

Upvotes: 4

Matt Ellen
Matt Ellen

Reputation: 11592

Thorsten Dittmar's answers is the way you should go.

However, with respect to this code:

foreach (Control c in this.Controls)
{
    if (c is TextBox && c.Name.StartsWith("sum"))
    {
        sumTextboxNames[y] = (TextBox)c;
        y++;
    }
}

You should try a solution that uses LINQ.

For example

TextBox [] sumTextBoxes = (from t in this.Controls.Cast<Control>
                          where t is TextBox
                          && t.Name.StartsWith("sum")
                          select t).Cast<TextBox>().ToArray();

Upvotes: 1

Related Questions