how to handle programmatically added button events? c#

前端 未结 6 1748
悲哀的现实
悲哀的现实 2020-12-03 07:39

I\'m making a windows forms application using C#. I add buttons and other controls programmatically at run time. I\'d like to know how to handle those buttons\' click events

6条回答
  •  半阙折子戏
    2020-12-03 08:19

    In regards to your comment saying you'd like to know which button was clicked, you could set the .Tag attribute of a button to whatever kind of identifying string you want as it's created and use

    private void MyButtonHandler(object sender, EventArgs e)
        {
            string buttonClicked = (sender as Button).Tag;
        }
    

提交回复
热议问题