C#自定义控件开发(1)
自定义控件可以用组件或者用户控件来开发,如果是基于基础控件,进行一些扩展,那么可以使用组件,其它的情况可以使用用户控件。
首先新建一个类库项目,然后添加一个组件,取名为ButtonExtend,再添加一个窗体进行测试:
因为是要在Button的基础上进行开发,所以将继承Component改为继承Button,同时将图片添加到资源中,方便使用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace JSControl
{
public partial class ButtonExtend : Button
{
public ButtonExtend()
{
InitializeComponent();
this.Size = new System.Drawing.Size(50,100);//初始化控件大小
} public ButtonExtend(IContainer container)
{
container.Add(this); InitializeComponent();
} #region Filed
private ButtonPresetImage buttonType; [Browsable(true)]
[Category("ButtonType")]
[Description("设置图片")]
public ButtonPresetImage ButtonType
{
get { return buttonType; }
set
{
switch (buttonType)
{
buttonType = value;
case ButtonPresetImage.None:
this.Image = null;
this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
break;
case ButtonPresetImage.Check:
this.Image = Properties.Resources.check;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Close:
this.Image = Properties.Resources.close;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Cancel:
this.Image = Properties.Resources.cancel;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Back:
this.Image = Properties.Resources.back;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Down:
this.Image = Properties.Resources.down;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Go:
this.Image = Properties.Resources.go;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Up:
this.Image = Properties.Resources.up;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Folder:
this.Image = Properties.Resources.folder;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Refresh:
this.Image = Properties.Resources.refresh;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Setting:
this.Image = Properties.Resources.setting;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.FolderOpen:
this.Image = Properties.Resources.folder_open;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.DocumentDelete:
this.Image = Properties.Resources.document_delete;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Document:
this.Image = Properties.Resources.document;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.DocumentEdit:
this.Image = Properties.Resources.document_edit;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Info:
this.Image = Properties.Resources.info;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.DocumentAdd:
this.Image = Properties.Resources.document_add;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Global:
this.Image = Properties.Resources.web;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Calculator:
this.Image = Properties.Resources.calculator;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Calendar:
this.Image = Properties.Resources.calendar;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Printer:
this.Image = Properties.Resources.printer;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
default:
break;
}
} }
#endregion /// <summary>
/// 枚举图标类型
/// </summary>
public enum ButtonPresetImage
{
None,
Check,
Close,
Cancel,
Back,
Down,
Go,
Up,
Folder,
Refresh,
Setting,
FolderOpen,
DocumentDelete,
Document,
DocumentEdit,
Info,
DocumentAdd,
Global,
Calculator,
Calendar,
Printer
}
} }
生成之后就会在工具箱找到该控件
同时在属性栏里可以选择图片
C#自定义控件开发(1)的更多相关文章
- iOS 自定义控件开发(中)
<iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> 接上篇iOS自定义控件开发之后,我们尝试另外一种. 在Xcode的右边,会看到如下的图 其中,上面有一个:C ...
- iOS 自定义控件开发(上)
工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> #0 目 ...
- C#自定义控件开发
自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足 ...
- 自定义控件开发的调试及DesignMode的状态处理
在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.但在使用的时候,我们又往往设计时刻发现一些莫名其妙的错误,那么我们该如 ...
- ZLComboBox自定义控件开发详解
[引言]距离上一回写博客已经有一些时日了,之前的爱莲iLinkIT系列主要是讲解了如何用NodeJS来实现一个简单的“文件传送”软件,属于JavaScript中在服务器端的应用. 今天,我们就回归到J ...
- .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)
大家好!我叫刘晶,很高兴你能看到我分享的文章!希望能对你有帮助! 首先我们来看下几个例子 ,就能看到 如何 自定义控件! 业务需求: 制作 一个 属于 自己的 按钮 对象 ,然后 像 ...
- TemplateBinding与Binding区别,以及WPF自定义控件开发的遭遇
在上一次的文章WPF OnApplyTemplate 不执行 或者执行滞后的疑惑谈到怎么正确的开发自定义控件,我们控件的样式中,属性的绑定一般都是用TemplateBinding来完成,如下一个基本的 ...
- Asp.net自定义控件开发任我行(附1)-属性一览众山小
元数据属性应用于服务器控件及其成员,从而提供由设计工具.ASP.NET 页分析器.ASP.NET 运行库以及公共语言运行库使用的信息.当页开发人员在可视化设计器中使用控件时,设计时属性能改进开发人员的 ...
- Asp.net自定义控件开发任我行(4)-ViewState保存控件状态
摘要 上一篇我们实现了下拉框的效果,此章的目的主要是保存控件属性状态 内容 我们先来看一个例子,后台代码不变,我们只改UI页面的代码,先在页面上拖放两个控件,一个是我们现在要开发的这个控件,另一个是按 ...
- Asp.net自定义控件开发任我行(2)-TagPrefix标签
摘要 前面我们已经做了一个最简单的TextBox的马甲,此篇文章,我们来讲讲自定义控件的标签.大家可能看到了上一篇中拖放进来的代码是 <cc1:TextEdit ID="TextEdi ...
随机推荐
- 状态 :睡眠中,进程ID:13431,yum提示Another app is currently holding the yum lock; waiting for it to exit...
问题描述: 今天想在虚拟机上重新安装docker然后使用到yum命令报错: 解决办法: [root@localhost ~]# rm -f /var/run/yum.pid 然后重新运行刚才的yum命 ...
- Configuration的学习
创建 //1.创建,调用的空惨 Configuration conf = new Configuration(); 加载主配置 //2.读取主配置文件==>如果是空参方法则自动加载sec下的re ...
- 第十二章 Kubernetes的服务暴露插件--traefik
1.前言 之前部署的coredns实现了k8s的服务在集群内可以被自动发现,那么如何使得服务在k8s集群外被使用和访问呢? 使用nodeport星的Service:此方法只能使用iptables模型, ...
- java 验证手机号是否合法
一.通用工具类编写 /** * @project * @Description * @Author songwp * @Date 2022/9/15 17:06 * @Version 1.0.0 ** ...
- 网络基础七层模型与TCP/IP协议
1.网络基础 1.1 什么是网络 网络就是计算机网络是一组计算机或网络设备通过有形 的线缆或无形的媒介如无线,连接起来,按照一定的 规则,进行通信的集合. 网络通信就是指终端设备之间通过计算机网络进行 ...
- 微服务系列之授权认证(三) JWT
1.JWT简介 官方定义:JWT是JSON Web Token的缩写,JSON Web Token是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,可以将各方之间的信息作为JSON ...
- Django 之视图层
JsonResponse 1 json格式的数据有什么用 前后端数据交互需要使用json作为过渡,实现跨语言传输数据 2 前后端方法对应 JSON.stringify() - json.dumps( ...
- Kubernetes 监控--Grafana
前面我们使用 Prometheus 采集了 Kubernetes 集群中的一些监控数据指标,我们也尝试使用 promQL 语句查询出了一些数据,并且在 Prometheus 的 Dashboard 中 ...
- ES 7.13版本设置索引模板和索引生命周期管理
第一步:索引管理中查看都有哪些索引文件,然后添加索引模式(后面的日期用*表示) 第二步:索引生命周期管理 自带的有一个log,就使用这个,不用再新建了,根据需求修改里面的配置就行了 第三步:添加索引模 ...
- K8S概念理解
Master 负责管理集群 负责协调集群中的所有活动,例如调度应用程序,维护应用程序的状态,扩展和更新应用程序. Worker节点是VM(虚拟机)或物理计算机,充当k8s集群中的工作计算机. 每个Wo ...