位置:首页 > 编程专栏 > 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 myUserControl_UserControlBtnClicked(object sender, EventArgs e)
        {
            MessageBox.Show("用户点击");
        }
    }

    /// <summary>
    /// 自定义控件
    /// </summary>
    public partial class MyUserControl : UserControl
    {
        // 定义委托
        public delegate void BtnClickHandle(object sender, EventArgs e);
        // 定义事件
        public event BtnClickHandle UserControlBtnClicked;

        private void UserControl1_Click(object sender, EventArgs e)
        {
            if (UserControlBtnClicked != null)
                UserControlBtnClicked(sender, new EventArgs());//把按钮自身作为参数传递
        }
    }
}


C# 社区推荐