Can someone "shortcut" this code for me?

I want this code to run from textBox1.textchanged to textBox170.textchanged (so this code should affect textbox1.textchanged/textbox2.textchanged/textbox3.textchanged....[e.t.c]....textbox170.textchanged):

string l_text;

            l_text = textBox1.Text;
            l_text += textBox2.Text;
            l_text += textBox3.Text;
            l_text += textBox4.Text;
            l_text += textBox5.Text;
            l_text += textBox6.Text;
            l_text += textBox7.Text;
            l_text += textBox8.Text;
            l_text += textBox9.Text;
            l_text += textBox10.Text;
            l_text += textBox11.Text;
            l_text += textBox12.Text;
            l_text += textBox13.Text;
            l_text += textBox14.Text;
            l_text += textBox15.Text;
            l_text += textBox16.Text;
            l_text += textBox17.Text;
            l_text += "\n";
            l_text += textBox18.Text;
            l_text += textBox19.Text;
            l_text += textBox20.Text;
            l_text += textBox21.Text;
            l_text += textBox22.Text;
            l_text += textBox23.Text;
            l_text += textBox24.Text;
            l_text += textBox25.Text;
            l_text += textBox26.Text;
            l_text += textBox27.Text;
            l_text += textBox28.Text;
            l_text += textBox29.Text;
            l_text += textBox30.Text;
            l_text += textBox31.Text;
            l_text += textBox32.Text;
            l_text += textBox33.Text;
            l_text += textBox34.Text;
            l_text += "\n";
            l_text += textBox35.Text;
            l_text += textBox36.Text;
            l_text += textBox37.Text;
            l_text += textBox38.Text;
            l_text += textBox39.Text;
            l_text += textBox40.Text;
            l_text += textBox41.Text;
            l_text += textBox42.Text;
            l_text += textBox43.Text;
            l_text += textBox44.Text;
            l_text += textBox45.Text;
            l_text += textBox46.Text;
            l_text += textBox47.Text;
            l_text += textBox48.Text;
            l_text += textBox49.Text;
            l_text += textBox50.Text;
            l_text += textBox51.Text;
            l_text += "\n";
            l_text += textBox52.Text;
            l_text += textBox53.Text;
            l_text += textBox54.Text;
            l_text += textBox55.Text;
            l_text += textBox56.Text;
            l_text += textBox57.Text;
            l_text += textBox58.Text;
            l_text += textBox59.Text;
            l_text += textBox60.Text;
            l_text += textBox61.Text;
            l_text += textBox62.Text;
            l_text += textBox63.Text;
            l_text += textBox64.Text;
            l_text += textBox65.Text;
            l_text += textBox66.Text;
            l_text += textBox67.Text;
            l_text += textBox68.Text;
            l_text += "\n";
            l_text += textBox69.Text;
            l_text += textBox70.Text;
            l_text += textBox71.Text;
            l_text += textBox72.Text;
            l_text += textBox73.Text;
            l_text += textBox74.Text;
            l_text += textBox75.Text;
            l_text += textBox76.Text;
            l_text += textBox77.Text;
            l_text += textBox78.Text;
            l_text += textBox79.Text;
            l_text += textBox80.Text;
            l_text += textBox81.Text;
            l_text += textBox82.Text;
            l_text += textBox83.Text;
            l_text += textBox84.Text;
            l_text += textBox85.Text;
            l_text += "\n";
            l_text += textBox86.Text;
            l_text += textBox87.Text;
            l_text += textBox88.Text;
            l_text += textBox89.Text;
            l_text += textBox90.Text;
            l_text += textBox91.Text;
            l_text += textBox92.Text;
            l_text += textBox93.Text;
            l_text += textBox94.Text;
            l_text += textBox95.Text;
            l_text += textBox96.Text;
            l_text += textBox97.Text;
            l_text += textBox98.Text;
            l_text += textBox99.Text;
            l_text += textBox100.Text;
            l_text += textBox101.Text;
            l_text += textBox102.Text;
            l_text += "\n";
            l_text += textBox103.Text;
            l_text += textBox104.Text;
            l_text += textBox105.Text;
            l_text += textBox106.Text;
            l_text += textBox107.Text;
            l_text += textBox108.Text;
            l_text += textBox109.Text;
            l_text += textBox110.Text;
            l_text += textBox111.Text;
            l_text += textBox112.Text;
            l_text += textBox113.Text;
            l_text += textBox114.Text;
            l_text += textBox115.Text;
            l_text += textBox116.Text;
            l_text += textBox117.Text;
            l_text += textBox118.Text;
            l_text += textBox119.Text;
            l_text += "\n";
            l_text += textBox120.Text;
            l_text += textBox121.Text;
            l_text += textBox122.Text;
            l_text += textBox123.Text;
            l_text += textBox124.Text;
            l_text += textBox125.Text;
            l_text += textBox126.Text;
            l_text += textBox127.Text;
            l_text += textBox128.Text;
            l_text += textBox129.Text;
            l_text += textBox130.Text;
            l_text += textBox131.Text;
            l_text += textBox132.Text;
            l_text += textBox133.Text;
            l_text += textBox134.Text;
            l_text += textBox135.Text;
            l_text += textBox136.Text;
            l_text += "\n";
            l_text += textBox137.Text;
            l_text += textBox138.Text;
            l_text += textBox139.Text;
            l_text += textBox140.Text;
            l_text += textBox141.Text;
            l_text += textBox142.Text;
            l_text += textBox143.Text;
            l_text += textBox144.Text;
            l_text += textBox145.Text;
            l_text += textBox146.Text;
            l_text += textBox147.Text;
            l_text += textBox148.Text;
            l_text += textBox149.Text;
            l_text += textBox150.Text;
            l_text += textBox151.Text;
            l_text += textBox152.Text;
            l_text += textBox153.Text;
            l_text += "\n";
            l_text += textBox154.Text;
            l_text += textBox155.Text;
            l_text += textBox156.Text;
            l_text += textBox157.Text;
            l_text += textBox158.Text;
            l_text += textBox159.Text;
            l_text += textBox160.Text;
            l_text += textBox161.Text;
            l_text += textBox162.Text;
            l_text += textBox163.Text;
            l_text += textBox164.Text;
            l_text += textBox165.Text;
            l_text += textBox166.Text;
            l_text += textBox167.Text;
            l_text += textBox168.Text;
            l_text += textBox169.Text;
            l_text += textBox170.Text;

            richTextBox1.Text = l_text;

As of now, I have that code written in EVERY textBox[number 1 -> 170].textchanged !This makes the program slower and makes it obviously a much bigger program (size wise)

I am pretty sure that you need an array class to do this but have never used array despite writing 35526 lines of code.

Thanks o TRiPPiNz,

P.s. also can someone explain to me what an array is and what it does. I know what a for loop does but could never get my head round people explaining arrays (mainly because I use YouTube for learning C#)

First, is the above block of code EXACTLY repeated in every textchanged event?

If so, the first step you can make towards reducing your code overhead is to take the above code block and place it into a separate function like so:

private void updRTB()
{
    string l_text = "";

    l_text = textBox1.Text;
    l_text += textBox2.Text;
    l_text += textBox3.Text;
    l_text += textBox4.Text;
    ...
    richTextBox1.Text = l_text;
}

and simply call that component from each textchanged event instead of having it appear separately in each one.

Second, unless you need a "real time" update of the RTB contents each time one of the 170 textboxes is changed you really don't need to fire it for every single textchanged event. However as this seems to be what you're going for I would further simplify by creating a collection out of the textboxes and cycling through that collection with the newline inserted every 10th cycle of the loop. Something like:

for (int a = 0; a < textBoxCollection.Count; a++)
{
    l_text += textBoxCollection[a].ToString();
    if ((a % 10) == 0)
    {
        l_text += "\n";
    }
}

But I leave it up to you to figure out the collection process :twisted:

Hope that helps :) Please remember to mark as solved once your issue is resolved.

And do I put the for loop right under the private void ?

Edited 6 Years Ago by trippinz: n/a

Can you divide your window into sections (like address info, preferences, interests), so you can query sections like this:

addressInfo.Fill(l_text);
preferences.Fill(l_text);
interests.Fill(l_text);

It will distribute your construction of the l_text variable over the sections and make the code less ugly by having less bigger methods.

I suggest, if it's the case, that you consider refactoring your code in more managable sections and class, instead of having one big class.

For the "for" loop, just put it in a method with a name like "BuildTextVar". It will look like this:

public string BuildTextVar()
{
string l_text;
//Insert for loop here.
return l_text;
}

Hope this helps!

Edited 6 Years Ago by GDICommander: n/a

As I said, my method above involves the creation and use of a collection class. Once the collection class is created all the textboxes get added to that collection in order to make them more easily iteratable (not even sure I spelt that right).

There's a lot more to the method than what I provided, I was just giving the direction that would work best based on the scenario you proposed at the top. However, I agree with GDICommander that breaking the page into more manageable bits might make more sense depending how you do it.

When you say you have it repeated in ALL of the textchanged events do you mean something like:

private void textBox1_TextChanged(object sender, EventArgs e)
{
            string l_text;
            l_text = textBox1.Text;
            l_text += textBox2.Text;
            l_text += textBox3.Text;
            ...
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
            string l_text;
            l_text = textBox1.Text;
            l_text += textBox2.Text;
            l_text += textBox3.Text;
            ...
}

private void textBox3_TextChanged(object sender, EventArgs e)
{
            string l_text;
            l_text = textBox1.Text;
            l_text += textBox2.Text;
            l_text += textBox3.Text;
            ...
}

....

If so you can cut a lot of code out by sharing the event handler. If you are executing the same code regardless of which textbox fires the event then set them all to have the same event handler. In otherwords, rename the method to something like:

private void textBoxGroup_TextChanged(object sender, EventArgs e)
{
}

Then in the designer, set the TextChanged event method to "textBoxGroup_TextChanged" for ALL of the textboxes.

Comments
Excellent point, thought of it but was getting tired of typing :P
This article has been dead for over six months. Start a new discussion instead.