Making an indexed control array?

后端 未结 7 1633
失恋的感觉
失恋的感觉 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:28

    Just create one handler and point all the buttons to it.

    var ButtonHandler = (sender, args) => {
        var clicked = (Button)sender;
        if (clicked.Text == "whatever")
           //do stuff
        else
           //do other stuff 
    };
    
    button1.Click += ButtonHandler;
    button2.Click += ButtonHandler;
    

    Alternatively, if you are creating controls in code, you could use one of the techniques specified in this answer.

提交回复
热议问题