Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / Windows Forms

How to copy event handlers from one control to another at runtime

Rate me:
Please Sign up or sign in to vote.
4.96/5 (11 votes)
1 Jan 2012CPOL4 min read 58.7K   2.4K   22  
This article will show you how to copy event handlers from one control to another at run time using dirty tricks and .NET Reflection.
  • how-to-copy-event-handlers-from-one-control-to-another-at-runtime.zip
    • how-to-copy-event-handlers-from-one-control-to-another-at-runtime
      • .svn
        • all-wcprops
        • entries
        • prop-base
        • props
        • text-base
          • HowToCopyEventHandlers.sln.svn-base
        • tmp
          • prop-base
          • props
          • text-base
      • Src
        • .svn
          • entries
          • prop-base
          • props
            • HowToCopyEventHandlers.suo.svn-work
          • text-base
          • tmp
            • prop-base
            • props
            • text-base
        • _ReSharper.HowToCopyEventHandlers
          • AspFileDataCache.dat
          • BuildScriptCache
            • .crc
            • .version
            • 9
              • d0c4926.dat
          • ConditionalNames
            • .crc
            • .version
            • 1
              • 59af2ab4.dat
          • JavaScriptCache
            • .crc
            • .version
            • 1
              • 195543cc.dat
          • JSTest
            • .crc
            • .version
          • ModuleIds.xml
          • MVC
            • .crc
            • .version
          • NamedArguments
            • .crc
            • .version
            • 1
              • 59af2ab4.dat
          • PersistenIdIndex.bin
          • Resources
            • .crc
            • .version
            • 8
              • 4b2cfc35.dat
          • SymbolCache.bin
          • TagPrefixes
            • .crc
            • .version
          • TodoCache
            • .crc
            • .version
            • 6
              • 2e6bc8c.dat
          • TodoItems
            • .crc
            • .version
            • 1
              • 195543cc.dat
          • WebSymbols
            • .crc
            • .version
          • WordIndex
            • .crc
            • .version
            • 1
              • 59af2ab4.dat
          • XmlIndex
            • Accessibility.xml
              • 091C23DC.bin
            • Microsoft.VisualC.xml
              • A81C801F.bin
            • mscorlib.2.0.0.0.Contracts.xml
              • 2C2FE705.bin
              • 5B7112C4.bin
            • mscorlib.2.0.0.0.Interfaces.Nullness.Generated.xml
              • 9075ABCC.bin
              • C418674D.bin
            • mscorlib.2.0.0.0.Nullness.Generated.xml
              • 84EF6F94.bin
              • E2B19D75.bin
            • mscorlib.2.0.0.0.Pure.xml
              • 1E801D57.bin
              • B22E2EB6.bin
            • mscorlib.2.0.5.0.Contracts.xml
              • C330A3C1.bin
            • mscorlib.2.0.5.0.Interfaces.Nullness.Generated.xml
              • 56983509.bin
            • mscorlib.2.0.5.0.Nullness.Generated.xml
              • 63ED2EB1.bin
            • mscorlib.2.0.5.0.Pure.xml
              • 0A810372.bin
            • mscorlib.4.0.0.0.Contracts.xml
              • 07FBF242.bin
            • mscorlib.4.0.0.0.Interfaces.Nullness.Generated.xml
              • 49D94A4A.bin
            • mscorlib.4.0.0.0.Nullness.Generated.xml
              • 83DB9ED2.bin
            • mscorlib.4.0.0.0.Pure.xml
              • CC017511.bin
            • mscorlib.I18n.xml
              • 366D6FC8.bin
              • 78F91969.bin
            • mscorlib.Nullness.Manual.xml
              • 4066AAC3.bin
              • A029C282.bin
            • mscorlib.ValueFlow.xml
              • 447F1EBE.bin
              • 4E59B0BF.bin
            • mscorlib.xml
              • 05998BE8.bin
              • 5A400175.bin
              • D6E16D29.bin
            • System.2.0.0.0.Contracts.xml
              • 66A824C4.bin
              • 929BDA85.bin
            • System.2.0.0.0.Nullness.Generated.xml
              • BE8780F5.bin
              • DC3AC194.bin
            • System.2.0.0.0.Pure.xml
              • 1483EF57.bin
              • 8CF3F236.bin
            • System.2.0.5.0.Contracts.xml
              • CE67B5C1.bin
            • System.2.0.5.0.Nullness.Generated.xml
              • BB3880B1.bin
            • System.2.0.5.0.Pure.xml
              • 0084D572.bin
            • System.4.0.0.0.Contracts.xml
              • 13330442.bin
            • System.4.0.0.0.Nullness.Generated.xml
              • DB26F0D2.bin
            • System.4.0.0.0.Pure.xml
              • C2054711.bin
            • System.Configuration.2.0.0.0.Contracts.xml
              • 93875E45.bin
              • A5A97A44.bin
            • System.Configuration.2.0.0.0.Nullness.Generated.xml
              • 13D44714.bin
              • FB5BB8B5.bin
            • System.Configuration.2.0.0.0.Pure.xml
              • 07AB3D76.bin
              • FA79D4D7.bin
            • System.Configuration.4.0.0.0.Contracts.xml
              • 523459C2.bin
            • System.Configuration.4.0.0.0.Nullness.Generated.xml
              • 12C07652.bin
            • System.Configuration.4.0.0.0.Pure.xml
              • A7FB2C91.bin
            • System.Configuration.Install.2.0.0.0.Contracts.xml
              • 6810E564.bin
              • BF2FCAE5.bin
            • System.Configuration.Install.2.0.0.0.Nullness.Generated.xml
              • 03AF4D15.bin
              • E1F3E774.bin
            • System.Configuration.Install.4.0.0.0.Contracts.xml
              • 69C465E2.bin
            • System.Configuration.Install.4.0.0.0.Nullness.Generated.xml
              • 9C0AD4B2.bin
            • System.Configuration.Install.xml
              • 7E7C4D46.bin
            • System.Configuration.xml
              • 3C5DA4E9.bin
            • System.Core.2.0.5.0.Contracts.xml
              • D5B29261.bin
            • System.Core.2.0.5.0.Nullness.Generated.xml
              • 74EC8511.bin
            • System.Core.2.0.5.0.Pure.xml
              • DB1DE692.bin
            • System.Core.3.5.0.0.Contracts.xml
              • 1D445C21.bin
              • EA155060.bin
            • System.Core.3.5.0.0.Nullness.Generated.xml
              • 585EA4D1.bin
              • FF82B2F0.bin
            • System.Core.3.5.0.0.Pure.xml
              • 00A63FF3.bin
              • C497F7D2.bin
            • System.Core.4.0.0.0.Contracts.xml
              • 9A26A5E2.bin
            • System.Core.4.0.0.0.Nullness.Generated.xml
              • FEF514B2.bin
            • System.Core.4.0.0.0.Pure.xml
              • 0304D931.bin
            • System.Core.LinqTunnel.xml
              • 63F26974.bin
              • 6C84E315.bin
            • System.Core.Nullness.Manual.xml
              • 31BDD0E2.bin
              • 6880D423.bin
            • System.Core.xml
              • 944CB108.bin
            • System.Data.2.0.0.0.Contracts.xml
              • 2C88F0E4.bin
              • C6AE46A5.bin
            • System.Data.2.0.0.0.Nullness.Generated.xml
              • A6F0E2F4.bin
              • A9765ED5.bin
            • System.Data.2.0.0.0.Pure.xml
              • 6AC59FF7.bin
              • A61761D6.bin
            • System.Data.4.0.0.0.Contracts.xml
              • 2E3C7162.bin
            • System.Data.4.0.0.0.Nullness.Generated.xml
              • 6107D032.bin
            • System.Data.4.0.0.0.Pure.xml
              • 0BED74B1.bin
            • System.Data.DataSetExtensions.3.5.0.0.Nullness.Generated.xml
              • 902F79F0.bin
              • A585CB51.bin
            • System.Data.DataSetExtensions.4.0.0.0.Nullness.Generated.xml
              • 8FA1DBB2.bin
            • System.Data.DataSetExtensions.I18n.xml
              • 77B72D89.bin
              • B16CE5E8.bin
            • System.Data.DataSetExtensions.xml
              • 48E09375.bin
            • System.Data.I18n.xml
              • 51BDCCE8.bin
              • C146C209.bin
            • System.Data.OracleClient.2.0.0.0.Nullness.Generated.xml
              • 7754BD95.bin
              • DA1ADAF4.bin
            • System.Data.OracleClient.4.0.0.0.Nullness.Generated.xml
              • 9431C832.bin
            • System.Data.OracleClient.xml
              • D650A468.bin
            • System.Data.SqlXml.2.0.0.0.Nullness.Generated.xml
              • 2B849E74.bin
              • 956E4495.bin
            • System.Data.SqlXml.4.0.0.0.Nullness.Generated.xml
              • E59B8BB2.bin
            • System.Data.SqlXml.xml
              • 2421DC30.bin
            • System.Data.xml
              • B986EE89.bin
            • System.Deployment.2.0.0.0.Nullness.Generated.xml
              • CEB490F4.bin
              • E0B9A455.bin
            • System.Deployment.4.0.0.0.Nullness.Generated.xml
              • 88CB7E32.bin
            • System.Deployment.xml
              • 54C70D00.bin
            • System.Design.2.0.0.0.Nullness.Generated.xml
              • 02690CB4.bin
              • 73443E95.bin
            • System.Design.4.0.0.0.Nullness.Generated.xml
              • 3D783EF2.bin
            • System.Design.xml
              • 2B0A890B.bin
            • System.DirectoryServices.2.0.0.0.Nullness.Generated.xml
              • 1B1757F4.bin
              • 78AE4555.bin
            • System.DirectoryServices.4.0.0.0.Nullness.Generated.xml
              • D52E4532.bin
            • System.DirectoryServices.Protocols.xml
              • B70A17F5.bin
            • System.DirectoryServices.xml
              • 99C5AFFE.bin
            • System.Drawing.2.0.0.0.Contracts.xml
              • AF2FFA85.bin
              • CAD0C484.bin
            • System.Drawing.2.0.0.0.Nullness.Generated.xml
              • 6F279A54.bin
              • 93BFA0F5.bin
            • System.Drawing.2.0.0.0.Pure.xml
              • 47C5B997.bin
              • 54741236.bin
            • System.Drawing.4.0.0.0.Contracts.xml
              • F7254502.bin
            • System.Drawing.4.0.0.0.Nullness.Generated.xml
              • 65618A92.bin
            • System.Drawing.4.0.0.0.Pure.xml
              • E8ED8F51.bin
            • System.Drawing.Design.2.0.0.0.Nullness.Generated.xml
              • A0A585B4.bin
              • CB20AA15.bin
            • System.Drawing.Design.4.0.0.0.Nullness.Generated.xml
              • DBB4B7F2.bin
            • System.Drawing.Design.xml
              • D632A645.bin
            • System.Drawing.xml
              • DD884EB9.bin
            • System.EnterpriseServices.2.0.0.0.Nullness.Generated.xml
              • D70EAD74.bin
              • F38E6055.bin
            • System.EnterpriseServices.4.0.0.0.Nullness.Generated.xml
              • 91259AB2.bin
            • System.EnterpriseServices.xml
              • 9352F9E2.bin
            • System.I18n.xml
              • 994AEB69.bin
              • E767B348.bin
            • System.Nullness.Manual.xml
              • 8EB13CC3.bin
              • AC02D602.bin
            • System.Runtime.Remoting.2.0.0.0.Nullness.Generated.xml
              • 357DCF15.bin
              • DF5CCDB4.bin
            • System.Runtime.Remoting.4.0.0.0.Nullness.Generated.xml
              • 1A6BFFF2.bin
            • System.Runtime.Remoting.xml
              • 90E3E38A.bin
            • System.Runtime.Serialization.Formatters.Soap.xml
              • 958ED99B.bin
            • System.Security.2.0.0.0.Contracts.xml
              • 132A5264.bin
              • 7E44A425.bin
            • System.Security.2.0.0.0.Nullness.Generated.xml
              • 25CDEC55.bin
              • 98F7F474.bin
            • System.Security.2.0.0.0.Pure.xml
              • 65241177.bin
              • 895D4F56.bin
            • System.Security.4.0.0.0.Contracts.xml
              • 14DDD2E2.bin
            • System.Security.4.0.0.0.Nullness.Generated.xml
              • 530EE1B2.bin
            • System.Security.4.0.0.0.Pure.xml
              • 064BE631.bin
            • System.Security.xml
              • 9B5E440F.bin
            • System.ServiceProcess.2.0.0.0.Nullness.Generated.xml
              • 3D91EDF4.bin
              • 64FAEAD5.bin
            • System.ServiceProcess.4.0.0.0.Nullness.Generated.xml
              • F7A8DB32.bin
            • System.ServiceProcess.xml
              • E3033A2F.bin
            • System.Transactions.2.0.0.0.Nullness.Generated.xml
              • 68818E55.bin
              • 80962574.bin
            • System.Transactions.4.0.0.0.Nullness.Generated.xml
              • 3AAD12B2.bin
            • System.Transactions.xml
              • 27D2270E.bin
            • System.Web.2.0.0.0.Contracts.xml
              • 110F7884.bin
              • BAEFDF05.bin
            • System.Web.2.0.0.0.Nullness.Generated.xml
              • 0930CE54.bin
              • B4E89575.bin
            • System.Web.2.0.0.0.Pure.xml
              • 8B03ED97.bin
              • 931326B6.bin
            • System.Web.4.0.0.0.Contracts.xml
              • 3D63F902.bin
            • System.Web.4.0.0.0.Nullness.Generated.xml
              • FF6ABE92.bin
            • System.Web.4.0.0.0.Pure.xml
              • 2C2BC351.bin
            • System.Web.Attributes.xml
              • C91B0A69.bin
              • FE2F6E88.bin
            • System.Web.I18n.xml
              • 152BB6A9.bin
              • DDDA67C8.bin
            • System.Web.RegularExpressions.xml
              • EFFF217C.bin
            • System.Web.Services.2.0.0.0.Nullness.Generated.xml
              • 5FB93F55.bin
              • EF3E8174.bin
            • System.Web.Services.4.0.0.0.Nullness.Generated.xml
              • A9556EB2.bin
            • System.Web.Services.Attributes.xml
              • 336EB089.bin
              • 6811A868.bin
            • System.Web.Services.xml
              • CBF00FA9.bin
            • System.Web.xml
              • 7FB6DEA9.bin
            • System.Windows.Forms.2.0.0.0.Contracts.xml
              • 28E48E05.bin
              • BFF1A7C4.bin
            • System.Windows.Forms.2.0.0.0.Nullness.Generated.xml
              • 1FE7A494.bin
              • 4E922475.bin
            • System.Windows.Forms.2.0.0.0.Pure.xml
              • 4A9E75B6.bin
              • 860F9257.bin
            • System.Windows.Forms.4.0.0.0.Contracts.xml
              • 6C7C8742.bin
            • System.Windows.Forms.4.0.0.0.Nullness.Generated.xml
              • 1ED3D3D2.bin
            • System.Windows.Forms.4.0.0.0.Pure.xml
              • 3390EA11.bin
            • System.Windows.Forms.I18n.xml
              • 4286B6C8.bin
              • B6E38E69.bin
            • System.Windows.Forms.Manual.xml
              • 4EEF7F3C.bin
              • CD0C231D.bin
            • System.Windows.Forms.xml
              • A586FAAD.bin
            • System.Xml.2.0.0.0.Contracts.xml
              • 1CF390E5.bin
              • 6BF6D6A4.bin
            • System.Xml.2.0.0.0.Nullness.Generated.xml
              • 6F949FB4.bin
              • B911D315.bin
            • System.Xml.2.0.0.0.Pure.xml
              • 8FAAEE37.bin
              • BEB7E496.bin
            • System.Xml.2.0.5.0.Contracts.xml
              • 551647A1.bin
            • System.Xml.2.0.5.0.Nullness.Generated.xml
              • A3D18051.bin
            • System.Xml.2.0.5.0.Pure.xml
              • 0328B352.bin
            • System.Xml.4.0.0.0.Contracts.xml
              • 4322B622.bin
            • System.Xml.4.0.0.0.Nullness.Generated.xml
              • AAA3D1F2.bin
            • System.Xml.4.0.0.0.Pure.xml
              • 3D2C46F1.bin
            • System.Xml.Attributes.xml
              • 09EED2C9.bin
              • 4A781A28.bin
            • System.Xml.I18n.xml
              • 8B551FA8.bin
              • BFE18249.bin
            • System.Xml.Linq.2.0.5.0.Contracts.xml
              • 176AE361.bin
            • System.Xml.Linq.2.0.5.0.Nullness.Generated.xml
              • 7D8FF611.bin
            • System.Xml.Linq.2.0.5.0.Pure.xml
              • 131F9792.bin
            • System.Xml.Linq.3.5.0.0.Contracts.xml
              • 2BCDA160.bin
              • 927AA021.bin
            • System.Xml.Linq.3.5.0.0.Nullness.Generated.xml
              • 082623F0.bin
              • D8E168D1.bin
            • System.Xml.Linq.3.5.0.0.Pure.xml
              • 38A7F0F3.bin
              • 89D3BBD2.bin
            • System.Xml.Linq.4.0.0.0.Contracts.xml
              • DBDEF6E2.bin
            • System.Xml.Linq.4.0.0.0.Nullness.Generated.xml
              • 079885B2.bin
            • System.Xml.Linq.4.0.0.0.Pure.xml
              • 3B068A31.bin
            • System.Xml.Linq.I18n.xml
              • 59625789.bin
              • A5756368.bin
            • System.Xml.Linq.xml
              • BA52F0DE.bin
            • System.Xml.xml
              • 729D3220.bin
            • System.xml
              • 74BB9F68.bin
              • 8DD176B7.bin
              • D647FF29.bin
        • HowToCopyEventHandlers.6.0.ReSharper.user
        • HowToCopyEventHandlers.6.1.ReSharper.user
        • HowToCopyEventHandlers.Sample
        • HowToCopyEventHandlers.sln
        • HowToCopyEventHandlers.sln.DotSettings.user
        • HowToCopyEventHandlers
  • DemoApp.zip
    • DemoApp
      • HowToCopyEventHandlers.dll
      • HowToCopyEventHandlers.Sample.exe
namespace HowToCopyEventHandlers.Sample {
    partial class Form1 {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(73, 19);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(73, 45);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 22);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Source";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(13, 49);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(60, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Destination";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(180, 19);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(157, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "Copy event handlers";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(349, 273);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions