简体   繁体   中英

Puting textboxes into a TextBox[] array

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

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.

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM