-1

I have code which generates buttons dynamically

Button txt = new Button();
this.Controls.Add(txt);
txt.Top = cleft * 40;
txt.Name = "txt_" + cb;
txt.Size = new Size(200, 16);
txt.Left = 150;

But I cannot think how to generate its click event.

3
  • I would do a quick google search on how to create Delegates you could also google how to get at an object from the (send) object Commented Nov 10, 2017 at 19:28
  • txt.Click += (sender, e) => {/* your code here */}; Commented Nov 10, 2017 at 19:31
  • @YvetteColomb Except I don't think that is the right duplicate. Commented Nov 10, 2017 at 19:31

1 Answer 1

2

I assume you don't mean "how to generate", but rather how to handle. Since you have a reference to your dynamic button, just add an event handler:

txt.Click += new EventHandler(eventHandlerFunction);

Or use a lambda:

txt.Click += (object sender, EventArgs e) => ...;
Sign up to request clarification or add additional context in comments.

1 Comment

Yess that is I meant!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.