It is not difficult to generate a Button. What is troublesome is its events. I have read a lot of decompiled code and many people's code on the Internet, which is different from what I think, currently, the execution and re-sending of control events are still vague, so you have time to study them. Unfortunately, there is no good book, so you can only study the code.
To put it bluntly, the online control generation has a drawback that all events are implemented through the event attribute form of the composite control, so it is impossible to dynamically add buttons and other widgets, however, if it is a fixed Composite Control, this is also quite good. My code is very simple, but it also took me a lot of time to explore. The main reason is that there are too few similar materials and the implementation is not perfect. If you don't want to talk about it, simply read the code (T. T );
public class testControl : CompositeControl { void bt_Click(object sender, EventArgs e) { EventHandler handler = (EventHandler)this.Events[sender]; if (handler != null) { handler(this, EventArgs.Empty); } } public void AddButton(string text, EventHandler e) { Button bt = new Button(); bt.Click +=new EventHandler(bt_Click); this.Events.AddHandler(bt, e); Controls.Add(bt); } }