// https://stackoverflow.com/questions/6055038/how-to-clone-control-event-handlers-at-run-time
// "C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe" /t:winexe /out:cloneevents.exe cloneevents.cs && start "cloneevents.exe" cloneevents.exe
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms; static class Program {
[STAThread]
public static void Main(params string[] args){
Application.Run(new Form1());
} public static void CloneEvents(Control targetControl, Control activeContorl) {
FieldInfo eventsField = typeof(Component).GetField("events", BindingFlags.NonPublic | BindingFlags.Instance);
object eventHandlers = eventsField.GetValue(targetControl);
eventsField.SetValue(activeContorl, eventHandlers);
}
} public class Form1 : Form {
Button _btn1= new Button { Text = "btn1", Left = , Top = , Width = };
Button _btn2 = new Button { Text = "clone btn1's click event", Left = , Top = , Width = }; public Form1() {
_btn1.Click+=(ss,se)=> MessageBox.Show(this, "btn1 is clicked.");
_btn2.Click+=(ss,se)=> {
Program.CloneEvents(_btn1, _btn2);
MessageBox.Show(this, "Clone btn1's events OK!\nClick btn2 again.");
};
this.Controls.Add(_btn1);
this.Controls.Add(_btn2);
this.Width = ;
this.Height = ;
}
}

(CSharp)克隆控件事件的更多相关文章

  1. (转)客户端触发Asp.net中服务端控件事件

    第一章. Asp.net中服务端控件事件是如何触发的 Asp.net 中在客户端触发服务端事件分为两种情况: 一. WebControls中的Button 和HtmlControls中的Type为su ...

  2. MFC 设置控件事件对应的函数

    在项目中,右击你想设置的控件,打开属性. 然后找到:控件事件,如果在Visual Studio 2015中操作,显示如图: 然后,以设定单击事件为例: 点击右边的三角,选择 <Edit Code ...

  3. RxJava RxBinding RxView 控件事件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. Python pyQt4/pyQt5 学习笔记1(空白窗口,按钮,控件事件,控件提示,窗体显示到屏幕中间,messagebox)

    PyQt4是用来编写有图形界面程序(GUI applications)的一个工具包.PyQt4作为一个Python模块来使用,它有440个类和超过6000种函数和方法.同时它也是一个可以在几乎所有主流 ...

  5. JQuery 点击子控件事件,不会触发父控件的事件

     $('.order-delete').on('tap', function (e) {                  console.log('删除1');                  c ...

  6. jquery on事件在IE8下失效的一种情况,及解决方法/bootstrap空间绑定控件事件不好用

    同事在复制bootstrap中的select控件之后,发现用$('.selectpicker').selectpicker();刷新下拉框控件不好使,后来发现是用原生js克隆的方法obj.cloneN ...

  7. Android开发之解决父控件拦截子控件事件问题

    以ViewPager为例: public class TopNewsViewPager extends ViewPager { public TopNewsViewPager(Context cont ...

  8. .net控件事件中的Sender

    private void button2_Click(object sender, RoutedEventArgs e) { } 最近看WPF内容,回顾下.net大家天天都在用,却不是十分关注的一个对 ...

  9. ASP.NET用户控件事件的定义和实践

    假定用户控件(UserControl.ascx)中包含按钮控件  AButton,希望实现按  Button  按钮时,包含该用户控件的页面可以接收到事件. UserControl.ascx.cs   ...

随机推荐

  1. myeclipse部署web项目部署按钮无效

    找到MyEclipse的工作路径,我的是“E:\Java”,到这个目录中去“\.metadata\.plugins\org.eclipse.core.runtime\.settings”找一个含有de ...

  2. ubuntu su failure when password was right

    https://blog.csdn.net/u013066244/article/details/52694540

  3. LeetCode232:Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...

  4. Java集合的遍历方式

    Map的遍历 1.通过map.entrySet遍历Key和Value Map<Integer,Integer> map = new HashMap<>(); map.put(1 ...

  5. 修改sql server实例、数据库、表、字段的排序规则

    转自:http://blog.51cto.com/jimshu/1095780 概念与详情请参考:字符编码与排序规则:https://www.cnblogs.com/gered/p/9145123.h ...

  6. 在Centos上打Preempt-rt实时补丁

    1.系统centos6.5,内核2.6.31.6,补丁patch-2.6.31.6-rt19.bz2,以下方式获得: wget https://www.kernel.org/pub/linux/ker ...

  7. zookeer安装

    解压:tar xf zookeeper-3.4.9.tar.gz进入目录cd /opt/zookeeper-3.4.9/ 编辑配置文件:vim zoo.cfg# The number of milli ...

  8. linux系统下nginx安装目录和nginx.conf配置文件目录

    linux系统下nginx安装目录和nginx.conf配置文件目录 1.查看nginx安装目录 输入命令 # ps  -ef | grep nginx 返回结果包含安装目录 root      26 ...

  9. PScc

      photoshop cc 2018破解版是Adobe公司最新版专业化CC图像处理软件,新功能超乎你的想象!photoshop cc 2018破解版新功能给力来袭,访问Lightroom照片,分享作 ...

  10. Shiro理解与总结

    Feature Apache Shiro is a comprehensive application security framework with many features. The follo ...