位置:首页 > 编程专栏 > C#

Winform 自定义控件, 自定义事件, 委托事件

Winform 自定义控件, 自定义事件, 委托事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void myUserControl1_TestClick(object sender, EventArgs e)
        {
            MessageBox.Show("用户点击2");
        }
    }

    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            // 注册事件
            this.Click += new System.EventHandler(this.MyUserControl_Click);
        }

        /// <summary>
        /// 自定义事件
        /// </summary>
        public event System.EventHandler TestClick;

        /// <summary>
        /// 单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyUserControl_Click(object sender, EventArgs e)
        {
            if (TestClick != null)
                TestClick(sender, e);
        }
    }
}

C# 社区推荐