I'm facing few problem on textbox :

  1. I got plenty of textbox which roughly 500+ and my design haven't done so it might grow. Each of the textbox is represent data received from serial port. To update textbox then i need to write the code line by line to update because the textbox name is not an array. So this will make my code become very long. Is there anyway to create textbox UI in array?

  2. When i update the textbox by code or by ui, the event "textchanged" also will detected. I know the solution is by setting a flag to differentiate is by code or ui. But since i got plenty of textbox. Creating 500+ textchanged function will make me mad. Or is there another thing that i can use to instead of textbox?

Just for further info, my textbox is just display some ascii / numeric within 10 char length.


6 Years
Discussion Span
Last Post by Momerath

Are these textBoxes on same form?
I would suggest you to create an array of textBoxes (in order, like you will populate them), and then just loop through them and fill text into each of them.

TextBox[] tbs;
public Form1()
    tbs = new TextBox[]{textBox1, textBox2, textBox3 }; //put all in here

public PopulateTextBoxes(string[] data)
       for(int i = 0; i < tbs.Lenght; i++)
           tbs[i].Text = data[i];

Call the method PopulateTextBoxes when you have all data gathered. I used string array, but you can have some other parameter, like a custom class, or what ever.

If there is so many textboxes, and this might consume some time to populate them all, you can create a new thread, and populate them there, but you will have to use delegates to do it so.


Also you only need one event handler and subscribe all the textboxes to it. The sender parameter will tell you which textbox was the one that changed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.