第一步 项目部分

  1. 新建项目在这里插入图片描述
  2. 选择窗体控件库 点击确定在这里插入图片描述
  3. 点击确定以后 出现一个空白区域 实际上这个空白区域已经是一个新建的控件 所以现在已经基本完成了 自定义控件的创建(如果不需要为这个控件自定义属性和事件可以直接跳到生成和配置部分生成控件了) 因为基本的属性和事件(例如click事件 backcolor属性等几乎每一个控件都需要有的属性和事件)系统已经帮你配置好了 下面的代码部分只是 为了增加 自定义的属性和事件

第二步 代码部分

  1. 在解决方案窗口中打开代码区
    在这里插入图片描述

自定义属性

定义普通属性
  1. 在代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControl2
{
    public partial class UserControl1: UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 控件的背景色
        /// </summary>
        private Color _backColorUC = Color.Transparent; //声明一个颜色变量 透明色 用于初始化 控件背景色
        [Description("控件的背景色")]  //新建控件说明 用来描述控件的作用
        public Color BackColorUC  //创建一个属性名 用于在属性窗口中显示出来
        {
            get { return _backColorUC; }  //返回 颜色变量 初始化背景色 为透明
            set                            //set 是当用户在属性窗口设置BackColorUC属性 选择颜色的时候执行
            {
                _backColorUC = value;  //获取用户在属性窗口中 选择的颜色 赋值给这个颜色变量 
                this.BackColor = _backColorUC;  //将用户选择的颜色赋值给 控件的背景颜色
            }
        }
    }
}

如果仅仅是打算自定义 普通属性的 可以 跳到 生成和配置部分 生成控件了 并且应用了 否则可以继续往下看

定义结合事件的属性

下面以 鼠标move事件后(鼠标移动到控件上发生该事件) 设置控件背景颜色 为例子
代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControl2
{
    public partial class UserControl1: UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 控件的背景色
        /// </summary>
        private Color _backColorUC = Color.Transparent; //声明一个颜色变量 透明色 用于初始化 控件背景色
        [Description("控件的背景色")]  //新建控件说明 用来描述控件的作用
        public Color BackColorUC  //创建一个属性名 用于在属性窗口中显示出来
        {
            get { return _backColorUC; }  //返回 颜色变量 初始化背景色 为透明
            set                                      //set 是当用户在属性窗口设置颜色的时候执行
            {
                _backColorUC = value;  //获取用户在属性窗口中 选择的颜色 赋值给这个颜色变量 
                this.BackColor = _backColorUC;  //将颜色赋值给 控件的背景颜色
            }
        }
        private Color _backcolorMove = Color.Transparent;
        [Description("光标移动到控件上方显示的颜色")]
        public Color backcolorMove						//再创建一个属性 用于接受 用户希望鼠标移动到 控件上时显示的颜色
        {
            get { return _backcolorMove; }
            set { _backcolorMove = value; }				//获取颜色 先不赋值
        }
        private void UserControl1_MouseMove(object sender, MouseEventArgs e)  //当用户将鼠标移动到控件上发生这个函数
        {
            if(_backcolorMove != Color.Transparent)      // 如果 用户在属性窗口为backcolorMove设置了颜色 _backColorUC 就不是原始的 透明色了 此时
            {
                BackColorUC = _backcolorMove;      //将用户设置的值赋值给BackColorUC 让它先赋值给_backColorUC再赋值给this.BackColor设置颜色
            }
        }
	}
}

如果仅仅是打算自定义 事件属性的 可以 跳到 生成和配置部分 生成控件 并且应用了 否则可以继续往下看

自定义事件

在同一个页面 写上这句代码

        public event EventHandler ButtonClick;  //声明一个事件
         private void UserControl1_Click(object sender, EventArgs e)  //该事件是寄托于控件本身的 点击事件 当控件被点击的时候发生这个事件
        {
            if (ButtonClick != null)                                      //当用户打开ButtonClick事件 的时候 ButtonClick 就不为空
            {
                ButtonClick(sender, e);                         //当用户点击该控件 时 执行 在ButtonClick 函数里面的代码
            }
        }

到这里 自定义控件 自定义控件的属性和事件 都完成了 可以在解决方案窗口 生成方案 了

第三步 生成和配置部分

为了以后我们在开发winform应用的过程中 能够在工具箱中找到自定义的控件并应用 就需要做 生成和配置部分
在这里插入图片描述
记住生成的地址 下面这个dll文件就是生成的自己定义的窗口控件库
在这里插入图片描述

  1. 新建一个winform项目(用于验证和配置 新建的控件)
    在这里插入图片描述
    在这里插入图片描述

  2. 在vs菜单栏中找到 选择工具项
    在这里插入图片描述
    在浏览中打开刚刚选中的dll文件 点击确定在这里插入图片描述
    再次打开工具箱 就可以找到 刚刚定义的控件了
    在这里插入图片描述

在窗口中创建该新控件 可以在属性窗口中看到 自己定义 的属性 也可以看到自己定义的事件 (也可以证实刚刚提到的 即使你不自己定义属性 系统都会帮你配置一些常用的属性和事件)
在这里插入图片描述

到这里 所有工作都完成了 别忘了尝试一下自己的成果哦

第四步 效果展示

在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐