Share via

Динамические кнопки c#

2021-05-24T03:36:41.167+00:00

Здравствуйте! Я хотел узнать, можно ли на динамически создаваемую кнопку привязать ивент button(X)_Click , который уже заранее описан?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,611 Reputation points
    2021-05-25T02:46:38.947+00:00

    Do you need code like the following?

            private void button1_Click(object sender, EventArgs e)  
            {  
                Button button = new Button();  
                button.Click += Button_Click;  
                this.Controls.Add(button);  
            }  
      
            private void Button_Click(object sender, EventArgs e)  
            {  
                MessageBox.Show("Hello");  
            }  
    

    Did I misunderstand what you mean? If so, please add some detailed description.

    Microsoft Q&A is an English forum, so please translate the text into English before providing it.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.