Making an indexed control array?

后端 未结 7 1656
失恋的感觉
失恋的感觉 2020-12-03 16:15

Has C# indexed control arrays or not? I would like to put a \"button array\" for example with 5 buttons which use just one event handler which handles the index of all this

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 16:25

    I know I'm a little late to this party, but this solution will work:

    Make a global array:

        TextBox[] myTextBox;
    

    Then in your object's constructor, after the call to

        InitializeComponent();
    

    initialize your array:

        myTextBox = new TextBox[] {TextBox1, TextBox2, ... };
    

    Now you can iterate your array of controls:

        for(int i = 0; i < myTextBox.Length; i++)
            myTextBox[i].Text = "OMG IT WORKS!!!";
    

    I hope this helps!

    Pete

提交回复
热议问题