0

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

The names are:

  • 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 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();  
        }  
   }
1
  • I think you need to clarify intent here, re-design. You parse a controls collection (WinForm I assume) : the order you "discover" the "Sum" TextBoxes ... I would guess ... has some meaning or purpose : they are probably visible (?) on the Form in some order (I hope). But parsing the Controls collection may not, reliably, give you the TextBoxes in an expected order. I suggest you think about having one List<TextBox> which you fill with the "sum_" TextBoxes, and two List<int>. Use one 'for loop and use the index to access the ints, add 'em up, convert result to string, assign, etc. best, Commented Nov 19, 2009 at 13:46

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the 1 and 2 shold be easy to fix, "string1 = "br" + i + "txt" + '1'; that shol make the string i need, but i still dont get how i can use this in the for loop you typed
<Name for the S-Textbox> = string3 in your code, and so on, DarkMage
oki diden get it to work whna tryed the but when i did a cast (textbox) its working just nice. so like TextBox s = (TextBox)this.Controls[string3]; i will put the code i used in new awnser for refrence
Edited my answer to include the missing casts :-)
1

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();

1 Comment

yes, LINQ looks very interesting, i will hav a look at this in the future.
1

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();
    }

1 Comment

Glad I could help. Just two more things: a) don't post a separate answer next time - just edit your question or post a comment (SO does not work like a forum, so the order of the answers will not always stay the same, which may cause confusion). b) why declare string1, string2 and string3 globally? Make them local to your SumX method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.