locked
Array of text boxes

    Question

  • I need to organize some text boxes into an array.  Assume I have six text boxes names A0 through A5.  I have some data to display in each.  The code to do this looks a bit like this:



    A0.Text = Convert.ToString( data[0] );
    A1.Text = Convert.ToString( data[1] );
    etc

     

    By the time we code up all six, and when more processing is needed, it gets rather ugly. I would like it to look more like this:



    for( x = 0; x < 6; x ++ )
    text_box[ x ].Text  = Convert.ToString( data[x] );

     

    Is there any way to do this with a fairly small amount of code?

     

    Tuesday, January 02, 2007 12:15 AM

Answers

  • Two approaches:

        TextBox[] mBoxes;
        private void Form1_Load(object sender, EventArgs e) {
          mBoxes = new TextBox[] {textBox1, textBox2, textBox3, textBox4, textBox5};
          // Iterate like this:
          foreach (TextBox box in mBoxes) {
            box.Text = "Hello world";
          }
        }

    Or, less desirable but doesn't require a member array:

          for (int ix = 1; ix <= 5; ++ix) {
            this.Controls["textBox" + ix.ToString()].Text = "Hello world2";
          }

    Tuesday, January 02, 2007 2:21 PM

All replies

  • You could try adding a Tag property to each of the textboxes and number them sequentially.  Then refer to the Tag value in the for loop.

     

    Tuesday, January 02, 2007 6:14 AM
  • Two approaches:

        TextBox[] mBoxes;
        private void Form1_Load(object sender, EventArgs e) {
          mBoxes = new TextBox[] {textBox1, textBox2, textBox3, textBox4, textBox5};
          // Iterate like this:
          foreach (TextBox box in mBoxes) {
            box.Text = "Hello world";
          }
        }

    Or, less desirable but doesn't require a member array:

          for (int ix = 1; ix <= 5; ++ix) {
            this.Controls["textBox" + ix.ToString()].Text = "Hello world2";
          }

    Tuesday, January 02, 2007 2:21 PM
  • Hello Solitate,

    I looked up Tag, found Control.Tag and did not understand it. I found the example here:

    ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref17/html/P_System_Windows_Forms_Control_Tag.htm

    Here is the example code with my questions



    private void buttonNewCustomer_Click(object sender, EventArgs e)
    {
            // Create an instance of the class named CustomerForm and name it customerForm
       CustomerForm customerForm = new CustomerForm();
     
            // create a new instance of class or object or  something called Customer and assign it (give it or put it in the variable )
           // found or accessed with the name customerForm.Tag.  
       customerForm.Tag = new Customer();
     
          // Call the method Show of the instantiated object customerForm.  I don't see any connection with Tag.
       customerForm.Show();
    }

     

    I don't understand what Tag is or does.

    Friday, January 05, 2007 2:18 AM
  • Hello nbugz,

    In the first example it looks to me that when the form loads, we create new text boxes in an array and set each with the text "Hello world"   Obviously other text can replace Hello world, but if the boxes are being created here, where are the characteristics of the box specified (the location, color, justication, etc)?  Or am I missing something?

    In the second example, we are nameing the text boxes with sequential numbers on the end,  then using the iterator (converted to text format) to build the next box name.  On one hand, that limits me to iterated names.  That leads to names that really don't mean much.  On the other hand, I have my other five fingers.  (LOL, I read that somewhere and couldn't resist passing it on).

    (enough with the idiot jokes).  I could create an array of strings, each entry is the name of a box, and use the contents of that string array as you did rather than building the name with an iterator. 

    Before I drive myself crazy trying to make either of these work, do I understand you correctly?  Is it possible to pull names out of an array and select text boxes for operation using a variable holding the name of the text box?

     

    Friday, January 05, 2007 2:35 AM
  • Hi, bk13

    As the way nobugz described, you can meet the need.

    In the first example, he creates an array of textbox contains two members that refer to two textboxes that have created in advance. Then you can control the members of the array as you can control the textboxes themselves.

    About second one, "this" refer to the form running, and it can retrieve from the control collection by the name of the control in [ ].

    If you want to iterate the name, you'd better use the first way, and remember to update the name when it changes.

     

    Friday, January 05, 2007 5:20 AM
  • Following up on Figo's answer; I do *not* create new text boxes in the array, I just store references to textboxes on the form into the array.  The "hello world" assignment was just there to show you how to iterate through the array.  The 2nd example shows how you can reference a control on the form by name.  The 1st sample is much preferable as it is can't generate exceptions and is not sensitive to what container hosts the control.
    Friday, January 05, 2007 5:17 PM