天道酬勤,学无止境

how to define an array of textboxes in c#?

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.

评论

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";
}

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;
}

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.

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.

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();

            }
}

TextBox[] t = new TextBox[10];
for(int i=0;i<required;i++)
{ 
   t[i]=new TextBox();
   this.Controls.Add(t[]);
}

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

相关推荐