这里的自定义控件是由普通控件组合而成的。
希望事件响应代码推迟到使用自定义控件的窗体里写。
步骤一:新建一个用户控件,放两个按钮,Tag分别是btn1,btn2.
这两个按钮的共用单击事件处理代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace UcDll
{
    public partial class UcTest : UserControl
    {
        public UcTest()
        {
            InitializeComponent();
        }         //定义委托
        public delegate void BtnClickHandle(object sender, EventArgs e);
        //定义事件
        public event BtnClickHandle UserControlBtnClicked;
        private void btn_Click(object sender, EventArgs e)
        {
            if (UserControlBtnClicked != null)
                UserControlBtnClicked(sender, new EventArgs());//把按钮自身作为参数传递
        }
    }
}
步骤二:当用户拖一个自定义控件在窗体的时候,
在事件里可以找到UserControlBtnClicked事件。

private void ucTest1_UserControlBtnClicked(object sender, EventArgs e)
{
    Button btn = sender as Button;
    MessageBox.Show(btn.Tag.ToString());
}
这个操作很有用。
url:http://greatverve.cnblogs.com/archive/2012/02/15/csharp-usercontrol-event.html

c# 自定义控件如何在属性栏添加自定义事件?可以双击生成+=代码?

用户控件的实现比较简单,直接从System.Windows.Forms.UserControl继承。

public class UserControl1 : System.Windows.Forms.UserControl

为了便于测试我在上面添加了一个TextBox,并注册TextBox的TextChanged事件,

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

事件处理函数,

private void textBox1_TextChanged(object sender, System.EventArgs e)

{

MessageBox.Show(this.textBox1.Text);

}

这里演示如果控件中文本框的内容改变就会用MessageBox显示当前的文本框内容。

窗体中添加上面的用户控件,当我们改变textBox的文本时,可以看到跳出一个对话框,很简单吧。

下面来看看对控件添加属性。
这里定义一个私有变量。
private string customValue;
添加访问他的属性
public string CustomValue
{
    get{return customValue;}
    set{customValue =value;}
}
在窗体中使用的时候像普通控件一样进行访问,
userControl11.CustomValue = "用户控件自定义数据";
通过事件可以传递消息到窗体上,在定义之前我们先来写一个简单的参数类。
public class TextChangeEventArgs : EventArgs
{
    private string message;
    public TextChangeEventArgs(string message)
    {
        this.message = message;
    }
public string Message
    {
        get{return message;}
    }
}
定义委托为,
public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
接下去在用户控件中添加事件,
//定义事件
public event TextBoxChangedHandle UserControlValueChanged;
为了激发用户控件的新增事件,修改了一下代码,
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
    if(UserControlValueChanged != null)
        UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
           
}
好了,为了便于在Csdn上回答问题,把完整的代码贴了出来:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
 
namespace ZZ.WindowsApplication1
{
    public class UserControl1 : System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.TextBox textBox1;
        private string customValue;
       
private System.ComponentModel.Container components = null;
 
        public string CustomValue
        {
            get{return customValue;}
            set{customValue =value;}
        }
 
        //定义事件
        public event TextBoxChangedHandle UserControlValueChanged;
 
        public UserControl1()
        {
            InitializeComponent();
        }
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        #region组件设计器生成的代码
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            this.textBox1.Location = new System.Drawing.Point(12, 36);
            this.textBox1.Name = "textBox1";
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "textBox1";
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            this.Controls.Add(this.textBox1);
            this.Name = "UserControl1";
            this.Size = new System.Drawing.Size(150, 92);
            this.ResumeLayout(false);
 
        }
        #endregion
 
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            if(UserControlValueChanged != null)
                UserControlValueChanged(this,new TextChangeEventArgs(this.textBox1.Text));
           
        }
    }
    //定义委托
    public delegate void TextBoxChangedHandle(object sender,TextChangeEventArgs e);
 
    public class TextChangeEventArgs : EventArgs
    {
        private string message;
        public TextChangeEventArgs(string message)
        {
            this.message = message;
        }
        public string Message
        {
            get{return message;}
        }
    }
}
使用时要在窗体中注册上面的事件,比较简单都贴源代码了,
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace ZZ.WindowsApplication1
{
    public class Form1 : System.Windows.Forms.Form
    {
        private WindowsApplication1.UserControl1 userControl11;
        private System.ComponentModel.Container components = null;
 
        public Form1()
        {
            InitializeComponent();
            userControl11.CustomValue = "用户控件自定义数据";
            userControl11.UserControlValueChanged += newTextBoxChangedHandle(userControl11_UserControlValueChanged);
        }
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        #region Windows 窗体设计器生成的代码
        private void InitializeComponent()
        {
            this.userControl11 = new WindowsApplication1.UserControl1();
            this.SuspendLayout();
            this.userControl11.Location = new System.Drawing.Point(8, 8);
            this.userControl11.Name = "userControl11";
            this.userControl11.Size = new System.Drawing.Size(150, 84);
            this.userControl11.TabIndex = 0;
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 193);
            this.Controls.Add(this.userControl11);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
 
        }
        #endregion
 
         [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
 
        private void userControl11_UserControlValueChanged(object sender, TextChangeEventArgs e)
        {
            MessageBox.Show("当前控件的值为:" + e.Message);
        }
    }
}
另外需要动态加载,就把控件添加在容器的Controls集合就行了,下面是在构造函数中添加控件,
public Form1()
{
    InitializeComponent();
    UserControl1 uc = new UserControl1();
    uc.CustomValue = "动态加载的用户控件";
    uc.UserControlValueChanged += new TextBoxChangedHandle(userControl11_UserControlValueChanged);
    this.Controls.Add(uc);
}
另外从VS.net中的工具箱中拖动用户控件到窗体上,如果是第一次需要编译一下项目。
 
//如果我有一个写好的控件,想在Form中使用如何???????
在控件中:  
        public delegate void OnSubBureauSelectChanged();//定义委托
        public event OnSubBureauSelectChanged onSubBureauSelectChanged;//定义事件
//以下代码放在你要用在窗体中调用的事件中,可以是控件中有的也可以自己写的
 if ( ( subBureaus.Count > 0 ) && ( onSubBureauSelectChanged != null ) )
                onSubBureauSelectChanged ();
//以下写在窗体构造中
searchPanel.onSubBureauSelectChanged += new SearchPanel.OnSubBureauSelectChanged ( OnSubBureauSelectChanged );
//以下再写一个自己写的事件
 private void OnSubBureauSelectChanged ( )
        {这样就可以了}

为C#自定义控件添加自定义事件的更多相关文章

  1. android 自定义控件之事件

    首先,继承需要扩展的VIEW,然后在里面添加一个自己的事件方法,例如, oniconclick(myinterface pinterface){ minterface = pinterface; } ...

  2. mfc添加自定义事件

    1.在对话框的头文件里面添加声明函数: afx_msg void OnStnClickedPicStop(); 2.在对话框的源文件添加 BEGIN_MESSAGE_MAP(CPcEn3dTestDl ...

  3. react 为元素添加自定义事件监听器

    https://zhenyong.github.io/react/tips/dom-event-listeners.html class MovieItem extends React.Compone ...

  4. Winform下让你的DataGridView控件支持点语法(即显示list中的子对象属性)

    前言: 不想看前言的直接去看正文吧!另外文末有彩蛋. DataGridView可以支持多种数据源格式,比如DataTable和List. DataTable没啥特殊的,本身就是一张二维的表,可以和Da ...

  5. C#中的自定义控件中的属性、事件及一些相关特性的总结(转)

      摘要: C#中的自定义控件中的属性(Property).事件(Event)及一些相关特性(Attribute)的总结 今天学习了下C#用户控件开发添加自定义属性的事件,主要参考了MSDN,总结并实 ...

  6. angularjs1 实现地图添加自定义控件(搜索功能)及事件

    // 添加地图自定义控件的事件 function addEventHandler(target, eventName, handler) { if (target.addEventListener) ...

  7. ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇

    原文:ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇 第三章 为控件添加事件 后篇 前一篇文章只是简单的说了下事件,但是大家应该方法,在ASP.NET自定义控件中只是简单那么定义事件是 ...

  8. WPF 创建自定义控件及自定义事件

    1 创建自定义控件及自定义事件 /// <summary> /// 演示用的自定义控件 /// </summary> public class ExtButton : Butt ...

  9. wpf自定义控件中使用自定义事件

    wpf自定义控件中使用自定义事件 1 创建自定义控件及自定义事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...

随机推荐

  1. 修改VMware虚拟机里面的显卡名称

    修改VMware虚拟机里面的显卡名称 现在有些蛋疼坑爹的游戏,会检测显卡的名称,如果其中有“Vmware”这类的字眼,就会提示程序在虚拟机中运行而拒绝执行(太不科学了嘛,我要是把我的实机驱动名称也改成 ...

  2. [No00007C]dreamweaver cc 注释快捷键

    设置快捷键入口:编辑->快捷键 打开后: 先复制一份快捷键拷贝(系统自带的不让修改), 选择代码编辑 下拉到后面,设置快捷键

  3. HBASE 安装法

    http://www.cnblogs.com/nexiyi/p/hbase_intro_94.html

  4. fcntl函数

    很多时候,当我们有多个进程要访问同一个文件的时候,为了防止多进程访问导致的不一致,我们就要考虑进程间的同步问题. fcntl是一个很强大的函数,我们可以通过它给文件的某一部分上锁 int fcntl( ...

  5. php 单双引号的区别

    在PHP中,字符串的定义可以使用英文单引号' ',也可以使用英文双引号" ". 但是必须使用同一种单或双引号来定义字符串,如:'Hello World"和"He ...

  6. C#的前世今生,学会C#还能找到高薪工作吗?

    其实C#,.net正在逐步淡出程序员的视野是正在发生的现实,量子及量子的小伙伴们,还在坚持写C#代码的人几乎没有了,回忆起过去那些写C#时候的美好时光,真是不胜唏嘘,最近园子里的一篇<C#程序员 ...

  7. github-创建版本库

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...

  8. sharedUserId

    android:sharedUserId sharedUserId的作用是让两个应用程序共享一个user id,我们都知道linux进程给每一个应用程序分配了一个独立的user id,所以如果两个或多 ...

  9. Advanced Office Password Recovery安装后显示是英文版的

    一些才开始接触Advanced Office Password Recovery(即AOPR)的朋友,在安装Advanced Office Password Recovery的时候可能发现Advanc ...

  10. IIS注册.netframework4.0指令

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i