locked
Create Text Box Array

    Question

  • Hi there,

    I'm trying to create an array of text boxes, so that I can easily iterate through them in a for loop, but I'm unable to figure out how to do this, either in the GUI designer or in the code.  Any help on how this can be accomplished would be greatly appreciated!

    Thanks!

    Tuesday, August 01, 2006 10:13 PM

Answers

  • you can do this programmatically:

     



    TextBox[] theTextBoxes = new TextBox[size];

     

     

    of course replace "size" with the number of textboxes you wish to create. This creates an array of textboxes of the size you have specified. 

    Then simply iterate through each one and do whatever you want with them, example:

     


     
    foreach (TextBox curControl in theTextBoxes)
    {
       //do stuff
    }

     
     

     

    OR 

    if you had several textboxes already on the form and you wish to navigate through them, try this:

     



    foreach (Control curControl in this.Controls)
    {
       if (curControl.GetType() == typeof(TextBox))
       {
          TextBox theTextBox = (TextBox)curControl;
          //do stuff
       }
    }

     

    does this help?

    Wednesday, August 02, 2006 2:21 AM
  • Hi,

    I guess your missing the Control Arrays in VB6. Well, you cannot do that in the GUI in .Net. But as ahmedilyas posted, you can do that in code. But the problem of this is you'll have to manually position your textboxes in your form. Tsk... it would get pretty long if you want to do this.

    There is a workaround on this. Try naming your textboxes with their last letters as numbers. ex:

    textBox1, textBox2, textBox3,...

    You can access them in your code this way:

    for (int i =1; i <= max_count; i++){
    TextBox curText = (TextBox) this.Controls["textBox" + i.ToString()];

    }

    voila! you can now loop your controls even though its not on array...

     

    cheers,

    Paul June A. Domag

    Wednesday, August 02, 2006 6:03 PM

All replies

  • you can do this programmatically:

     



    TextBox[] theTextBoxes = new TextBox[size];

     

     

    of course replace "size" with the number of textboxes you wish to create. This creates an array of textboxes of the size you have specified. 

    Then simply iterate through each one and do whatever you want with them, example:

     


     
    foreach (TextBox curControl in theTextBoxes)
    {
       //do stuff
    }

     
     

     

    OR 

    if you had several textboxes already on the form and you wish to navigate through them, try this:

     



    foreach (Control curControl in this.Controls)
    {
       if (curControl.GetType() == typeof(TextBox))
       {
          TextBox theTextBox = (TextBox)curControl;
          //do stuff
       }
    }

     

    does this help?

    Wednesday, August 02, 2006 2:21 AM
  • Hi,

    I guess your missing the Control Arrays in VB6. Well, you cannot do that in the GUI in .Net. But as ahmedilyas posted, you can do that in code. But the problem of this is you'll have to manually position your textboxes in your form. Tsk... it would get pretty long if you want to do this.

    There is a workaround on this. Try naming your textboxes with their last letters as numbers. ex:

    textBox1, textBox2, textBox3,...

    You can access them in your code this way:

    for (int i =1; i <= max_count; i++){
    TextBox curText = (TextBox) this.Controls["textBox" + i.ToString()];

    }

    voila! you can now loop your controls even though its not on array...

     

    cheers,

    Paul June A. Domag

    Wednesday, August 02, 2006 6:03 PM
  • These were both very helpful, thank you very much!
    Wednesday, August 02, 2006 8:30 PM
  • Hi Paul,

    I have a series of textbox..the solution to my problem is the solution you mentioned. but it gives different result in my database (MS Access)

     

    Public Sub savelist()
      Dim N, D, U, Q, P, T As String 'these are the names of my textbox
                          'N for Number (N1.text, N2.text...)
                          'D-Description (D1.text, D2.text...)
                          'U-Unit (U1.text, U2.text....U14.text)
                          ' Quantity (Q1.text. Q2.text...Q14.text)
                          'Price (P) and Total (T)...
      Dim counter As Integer
      Connect    'connect to database module
    
      For counter = 1 To 14
    
          sql = "INSERT INTO POitems (POno,Description,DUnit,Dquantity,DPUnit,DAmount) Values(" & Text3 & ",'" & (D & counter) & "','" & (U & counter) & "'," & (Q & counter) & "," & (P & counter) & "," & (T & counter) & ")"
    
        ConnDB.Conn.Execute sql
    
      Next counter
    
      ConnDB.Conn.Close
    
    
    
    what can you say about my code? please help...  thank  you.,

     

     

    Tuesday, January 04, 2011 5:41 AM
  •  Ran into the same issue took a slightly path  LINQ

     This line was the key:   let r =new string(g.Where(char.IsDigit).ToArray())      Really useful Lambda returns digits in a string

    Example IF  var g= "DAFSGS5kaka6AKAKA7";   var  r =new string(g.Where(char.IsDigit).ToArray()) 

    would return  "567"

    In the code sample I have 

      var klength = nCounter;
                int nRowcount = 0;
                nRowcount = ds.Tables[0].Rows.Count;
              
                for (int i = 1; i < nRowcount; i++)
            {    
               

                    var res = from box in frmContainer.Controls.OfType<TextBox>()
                                  let g = box.Name
                                  let r =new string(g.Where(char.IsDigit).ToArray())                             
                                  where r == i.ToString() &&
                                  (
                                      box.Name.Contains("Cid") == true
                                      || box.Name.Contains("CCCharge") == true
                                      || box.Name.Contains("InHouse") == true
                                      || box.Name.Contains("GifCard") == true
                                      || box.Name.Contains("Deposit") == true
                                      || box.Name.Contains("Return") == true
                                      || box.Name.Contains("CU") == true
                                      || box.Name.Contains("ClerkName") == true

                              )
                                  orderby box.Name

                           
                              select box;

    LINQ expression OMG not used on a DB!

    Steve Martin (BionicBorg)

    Thursday, November 15, 2012 5:40 PM
  • Array by itself doesn't help have to initialize the object.

    I am doing this myself stubbing this out myself

        public partial class Form1 : Form
        {

            TextBox[] bunchOfText = new TextBox[0];
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                int txtLeft=this.textBox1.Left;
                int v = textBox1.Height;
                int verticalOfset=30 + textBox1.Height;
                verticalOfset = LoadArray(txtLeft, verticalOfset, 5);
            }

            private int LoadArray(int txtLeft, int verticalOfset, int sizeArray)
            {
                Array.Resize(ref bunchOfText, sizeArray);
                int length = bunchOfText.Length;

                for (int i = 0; i < length; i++)
                {

                    bunchOfText[i] = new TextBox();
                    bunchOfText[i].Location = new System.Drawing.Point(txtLeft, verticalOfset);
                    verticalOfset += 25;

                    bunchOfText[i].Size = new System.Drawing.Size(100, 20);
                    this.Controls.Add(bunchOfText[i]);

                }

                return verticalOfset;
            }

    BionicCyborg

    Steve Martin (Bionic....

               
    Thursday, November 15, 2012 7:24 PM