自定义控件可以用组件或者用户控件来开发,如果是基于基础控件,进行一些扩展,那么可以使用组件,其它的情况可以使用用户控件。

首先新建一个类库项目,然后添加一个组件,取名为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)的更多相关文章

  1. iOS 自定义控件开发(中)

    <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> 接上篇iOS自定义控件开发之后,我们尝试另外一种. 在Xcode的右边,会看到如下的图 其中,上面有一个:C ...

  2. iOS 自定义控件开发(上)

    工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> #0 目 ...

  3. C#自定义控件开发

    自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足 ...

  4. 自定义控件开发的调试及DesignMode的状态处理

    在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.但在使用的时候,我们又往往设计时刻发现一些莫名其妙的错误,那么我们该如 ...

  5. ZLComboBox自定义控件开发详解

    [引言]距离上一回写博客已经有一些时日了,之前的爱莲iLinkIT系列主要是讲解了如何用NodeJS来实现一个简单的“文件传送”软件,属于JavaScript中在服务器端的应用. 今天,我们就回归到J ...

  6. .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)

    大家好!我叫刘晶,很高兴你能看到我分享的文章!希望能对你有帮助! 首先我们来看下几个例子 ,就能看到 如何 自定义控件! 业务需求: 制作  一个   属于 自己的    按钮 对象    ,然后 像 ...

  7. TemplateBinding与Binding区别,以及WPF自定义控件开发的遭遇

    在上一次的文章WPF OnApplyTemplate 不执行 或者执行滞后的疑惑谈到怎么正确的开发自定义控件,我们控件的样式中,属性的绑定一般都是用TemplateBinding来完成,如下一个基本的 ...

  8. Asp.net自定义控件开发任我行(附1)-属性一览众山小

    元数据属性应用于服务器控件及其成员,从而提供由设计工具.ASP.NET 页分析器.ASP.NET 运行库以及公共语言运行库使用的信息.当页开发人员在可视化设计器中使用控件时,设计时属性能改进开发人员的 ...

  9. Asp.net自定义控件开发任我行(4)-ViewState保存控件状态

    摘要 上一篇我们实现了下拉框的效果,此章的目的主要是保存控件属性状态 内容 我们先来看一个例子,后台代码不变,我们只改UI页面的代码,先在页面上拖放两个控件,一个是我们现在要开发的这个控件,另一个是按 ...

  10. Asp.net自定义控件开发任我行(2)-TagPrefix标签

    摘要 前面我们已经做了一个最简单的TextBox的马甲,此篇文章,我们来讲讲自定义控件的标签.大家可能看到了上一篇中拖放进来的代码是 <cc1:TextEdit ID="TextEdi ...

随机推荐

  1. 并发编程二、CPU多级缓存架构与MESI协议的诞生

    ​前言: 文章内容:线程与进程.线程生命周期.线程中断.线程常见问题总结 本文章内容来源于笔者学习笔记,内容可能与相关书籍内容重合 偏向于知识核心总结,非零基础学习文章,可用于知识的体系建立,核心内容 ...

  2. Python代码用在这些地方,其实1行就够了!

    摘要:都说 Python 简单快捷,那本篇博客就为大家带来一些实用的 Python 技巧,而且仅需要 1 行代码,就可以解决一些小问题. 本文分享自华为云社区<你猜 1 行Python代码能干什 ...

  3. KingbaseES V8R6集群同步模式synchronous参数配置详解

    如下图所示: 集群数据同步原理说明: synchronous参数配置测试: 集群节点信息: ID | Name | Role | Status | Upstream | repmgrd | PID | ...

  4. Java 流处理之收集器

    Java 流(Stream)处理操作完成之后,我们可以收集这个流中的元素,使之汇聚成一个最终结果.这个结果可以是一个对象,也可以是一个集合,甚至可以是一个基本类型数据. 以记录 Record 为例: ...

  5. Linux服务器上MinIO生产部署的内核调优

    #!/bin/bash cat > sysctl.conf <<EOF # maximum number of open files/file descriptors fs.file ...

  6. mysql8数据库修改root密码,以及创建用户遇到的坑,开启远程登录,用navicat进行mysql的远程连接,mysql8.0默认编码方式,部分参数配置查询命令

    yum 安装MySQL8 echo "删除系统默认或之前可能安装的其他版本的 mysql" for i in $(rpm -qa|grep mysql);do rpm -e $i ...

  7. LeetCode - 二维数组及滚动数组

    1. 二维数组及滚动数组总结 在二维数组num[i][j]中,每个元素都是一个数组.有时候,二维数组中的某些元素在整个运算过程中都需要用到:但是有的时候我们只需要用到前一个或者两个数组,此时我们便可以 ...

  8. 一篇文章带你掌握主流服务层框架——SpringMVC

    一篇文章带你掌握主流服务层框架--SpringMVC 在之前的文章中我们已经学习了Spring的基本内容,SpringMVC隶属于Spring的一部分内容 但由于SpringMVC完全针对于服务层使用 ...

  9. python之流程控制上-if、while

    流程控制 编写程序,是将自己的逻辑思想记录下来,使得计算机能够执行的过程. 而流程控制,则是逻辑结构中十分重要的一环. 在程序中,基础的流程结构分为顺序结构.分支结构.顺序结构 顺序结构自不必多说,上 ...

  10. 无法创建“Sunlight.Silverlight.Dcs.Web.PartsSupplier”类型的常量值。此上下文仅支持基元类型或枚举类型问题

    今天写代码遇到一个问题,            const string SCODE = "123";            var suppliers = PartsSuppli ...