How do I make a Control Array in C# 2010.NET?

前端 未结 5 1400
故里飘歌
故里飘歌 2020-12-07 03:19

I recently moved from Visual Basic 6 to C# 2010 .NET.

In Visual Basic 6 there was an option to put how many control arrays you would like to use by changing the \"i

5条回答
  •  渐次进展
    2020-12-07 03:45

    That code snippet isn't going to get you very far. Creating a control array is no problem, just initialize it in the form constructor. You can then expose it as a property, although that's generally a bad idea since you don't want to expose implementation details. Something like this:

    public partial class Form1 : Form {
        private TextBox[] textBoxes;
    
        public Form1() {
            InitializeComponent();
            textBoxes = new TextBox[] { textBox1, textBox2, textBox3 };
        }
    
        public ICollection TextBoxes {
            get { return textBoxes; }
        }
    }
    

    Which then lets you write:

    var form = new Form1();
    form.TextBoxes[0].Text = "hello";
    form.Show();
    

    But don't, let the form manage its own text boxes.

提交回复
热议问题