7

Hi when I create textboxes on Windows Application Form I cannot name it as box[0], box[1] and so on. The purpose why I want to do like this is because I want to use them in a loop.

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103

7 Answers7

9

How about making a list of them after you create them? In your form initialization function, you can do something like:

List<TextBox> myTextboxList = new List<TextBox>();
myTextBoxList.Add(TextBox1);
myTextBoxList.Add(TextBox2);
mytextBoxList.Add(TextBox3);

Now you can itterate through with your "myTextboxList" with something like below:

Foreach (TextBox singleItem in myTextboxList) {
    // Do something to your textboxes here, for example:
    singleItem.Text = "Type in Entry Here";
}
Jeff Halverson
  • 253
  • 1
  • 8
  • Yes, List would probably be better. You can add/remove textboxes easier. – Mert Akcakaya May 27 '12 at 01:52
  • where is that function? public MainForm() { InitializeComponent(); } this one? If so giving out an error... – Sarp Kaya May 27 '12 at 02:23
  • By the way how do I run this? does not work like myTextBoxList[0] – Sarp Kaya May 27 '12 at 02:26
  • @SarpKaya put it after the InitializeComponent(); – Cole Tobin May 27 '12 at 02:48
  • Your form initialization point will depend on what type of application you are making... It is windows, Gtk, Mac, etc.... Typically there will be an entry point to where you code starts, and you can include this there... If you aren't using an IDE, and are manually creating all your textboxes, you can simply use this code directly after you make your textboxes. – Jeff Halverson May 27 '12 at 06:16
  • I've updated my entry to show how you will then iterate through these. Because I used Generic List function and not an Array, you will access like I wrote, not with myTextBoxList[0] type of code. – Jeff Halverson May 27 '12 at 06:20
9

Actually I found TextBox[] array = { firstTextBox, secondTextBox }; works too!

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
2

You can create textboxes on runtime and just put them in an array...

If you want to do it in design time, you will have to do some control filtering logic on the whole this.Controls array in order to access only the wanted textboxes. Consider if (currControl is TextBox) if all textboxes in the form are ones you want in the array.

Another option for design time, is putting all wanted textboxes in a panel which will be their parent, and then iterating over the panel's children (controls) and cast them to TextBox.

A runtime solution would be something like:

var arr = new TextBox[10];

for (var i = 0; i < 10; i++)
{
    var tbox = new TextBox();

    // tbox.Text = i.ToString();

    // Other properties sets for tbox

    this.Controls.Add(tbox);
    arr[i] = tbox;
}
SimpleVar
  • 14,044
  • 4
  • 38
  • 60
0

I wouldn't use an array for this, personally. I would use some form of generic collection, like List.

    List<TextBox> textBoxList = new List<TextBox>();

    //Example insert method
    public void InsertTextBox(TextBox tb)
    {
        textBoxList.Add(tb);
    }

    //Example contains method
    public bool CheckIfTextBoxExists(TextBox tb)
    {
        if (textBoxList.Contains(tb))
            return true;
        else
            return false;
    }

You don't necessarily have to use the Contains method, you could also use Any(), or maybe even find another way- all depends on what you're doing. I just think using a generic collection gives you more flexibility than a simple array in this case.

William Smith
  • 1,949
  • 4
  • 25
  • 45
0

for C# just use this to create an array of text boxes

public Text [] "YourName" = new Text ["how long you want the array"];

then add the text boxes to the array individually.

Ghasem
  • 14,455
  • 21
  • 138
  • 171
Declan
  • 1
0

TextBox Array using C#

 // Declaring array of TextBox
private System.Windows.Forms.TextBox[] txtArray;

private void AddControls(int cNumber)
{

            // assign number of controls

            txtArray = new System.Windows.Forms.TextBox[cNumber + 1]; 

            for (int i = 0; i < cNumber + 1; i++)

            {

                        // Initialize one variable

                        txtArray[i] = new System.Windows.Forms.TextBox();

            }
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171
-1
TextBox[] t = new TextBox[10];
for(int i=0;i<required;i++)
{ 
   t[i]=new TextBox();
   this.Controls.Add(t[]);
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38