目录
2.1,使用特性将自定义属性添加到属性窗口并进行注解,分类。
1,创建类库添加自定义控件。
1.1,引用程序集
System.Drawing程序集,System.Windows.Froms程序集。
1.2,添加自定义控件类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace WinFormCustomControlLibrary
{
[ToolboxBitmap(typeof(SuperTextBox), "tubiao.ico")]
public class SuperTextBox : TextBox
{
public SuperTextBox()
{
}
//创建一个错误提示Errorprovider
ErrorProvider provider = new ErrorProvider();
/// <summary>
/// 对输入内容进行正则表达式验证
/// </summary>
/// <param name="regularExpression">正则表达式</param>
/// <param name="errorInfo">提示的错误信息</param>
/// <returns></returns>
public bool ValidationRule(string regularExpression, string errorInfo)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(this.Text, regularExpression))
{
provider.SetError(this, errorInfo);
return false;
}
provider.SetError(this, null);
return true;
}
}
}
1.3,变更默认的自定义控件图标。
1.3.1,控件图标要求。
大小:控件的图标必须是 16x16 位图图像。
文件类型:图标可以是位图 (.bmp) 或 Windows 图标 (.ico) 文件。
透明度:洋红色(RGB:255,0,255,Hex:0xFF00FF)呈现透明。
主题:Visual Studio 有多个主题,但每个主题要么是深色,要么是浅色。 图标应设计为浅色主题。 当 Visual Studio 使用深色主题时,图标中的深色和浅色将自动反转。
1.3.2,添加控件图标文件。
将图标图片复制到程序根目录下,在解决方案管理器中在项目名称右键选择“添加”—>“添加现有项”,将图标图片加入到项目中,对图片资源的属性进行修改,将“生成操作”一栏该为“嵌入的资源”。
1.3.3,自定义类附加特性变更图标。