我想复制一个控件,如 Button、TextBox 等.但我不知道如何将事件处理程序方法(如 Click
)复制到新控件.
I want to duplicate a control like a Button, TextBox, etc.
But I don't know how I can copy event handler methods (like Click
) to the new control.
我现在有以下代码:
var btn2 = new Button();
btn2.Text = btn1.Text;
btn2.size = btn1.size;
// ...
btn2.Click ??? btn1.Click
还有其他方法可以复制控件吗?
Is there any other way to duplicate a control?
克隆任何 WinForms 控件的所有事件:
To clone all events of any WinForms control:
var eventsField = typeof(Component).GetField("events", BindingFlags.NonPublic | BindingFlags.Instance);
var eventHandlerList = eventsField.GetValue(button1);
eventsField.SetValue(button2, eventHandlerList);
这篇关于如何在运行时克隆 Control 事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!