What's the simplest .NET equivalent of a VB6 control array?

后端 未结 9 1713
时光取名叫无心
时光取名叫无心 2020-12-11 03:42

Maybe I just don\'t know .NET well enough yet, but I have yet to see a satisfactory way to implement this simple VB6 code easily in .NET (assume this code is on a form with

9条回答
  •  盖世英雄少女心
    2020-12-11 03:46

    Make a generic list of textboxes:

    var textBoxes = new List();
    
    // Create 10 textboxes in the collection
    for (int i = 0; i < 10; i++)
    {
        var textBox = new TextBox();
        textBox.Text = "Textbox " + i;
        textBoxes.Add(textBox);
    }
    
    // Loop through and set new values on textboxes in collection
    for (int i = 0; i < textBoxes.Count; i++)
    {
        textBoxes[i].Text = "New value " + i;
        // or like this
        var textBox = textBoxes[i];
        textBox.Text = "New val " + i;
    }
    

提交回复
热议问题