C#继承基本控件实现自定义控件

摘自:http://www.cnblogs.com/greatverve/archive/2012/04/25/user-control-inherit.html

自定义控件分三类:
1.复合控件:基本控件组合而成。继承自UserControl
2.扩展控件:继承基本控件,扩展一些属性与事件。比如继承Button
3.自定义控件:直接继承自Control
第一种情况上手比较容易,也比较常用,其中也有不少技巧,慢慢总结。
比如要单独建个类库项目,才不会引起冲突。
怎样把事件代码推迟到使用者。
今天把扩展控件简单入门。
------------------------------------------------------------------
步骤一:这里首先要建一个Windows控件库项目。
步骤二:新建用户控件,修改代码(注意注释掉.Designer.cs文件中的代码)
扩展Button


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WinFormControlLibrary
{
    public partial class UcButton : Button
    {
        public UcButton()
        {
            InitializeComponent();
        }
        // Creates the private variable that will store the value of your 
        // property.
        private int varValue;
        // Declares the property.
        public int ButtonValue
        {
            // Sets the method for retrieving the value of your property.
            get
            {
                return varValue;
            }
            // Sets the method for setting the value of your property.
            set
            {
                varValue = value;
            }
        }
    }
}

修改.Desinger.cs


namespace WinFormControlLibrary
{
    partial class UcButton
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;         /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }         #region Component Designer generated code         /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            //把这句注释掉
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }         #endregion
    }
}

扩展Label


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WinFormControlLibrary
{
    public partial class UcLabel : Label
    {
        public UcLabel()
        {
            InitializeComponent();
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Font = new Font("宋体", 10F, FontStyle.Underline);
        }
        protected override void OnMouseLeave(System.EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Font = new Font("宋体", 10F, FontStyle.Regular);
        }
    }
}

步骤三:在其他Windows窗体项目中添加项目引用。编译之后就在工具箱看到生成的自定义控件。
url:http://greatverve.cnblogs.com/archive/2012/02/16/user-control-Inherit.html
参考msdn:

程序员的网店:http://shop108042866.taobao.com

C#继承基本控件实现自定义控件的更多相关文章

  1. C#继承基本控件实现自定义控件 (转帖)

    自定义控件分三类: 1.复合控件:基本控件组合而成.继承自UserControl 2.扩展控件:继承基本控件,扩展一些属性与事件.比如继承Button 3.自定义控件:直接继承自Control 第一种 ...

  2. Android 自定义View 三板斧之一——继承现有控件

    通常情况下,Android实现自定义控件无非三种方式. Ⅰ.继承现有控件,对其控件的功能进行拓展. Ⅱ.将现有控件进行组合,实现功能更加强大控件. Ⅲ.重写View实现全新的控件 本文重点讨论继承现有 ...

  3. [UE4]继承标准控件

    可以继承自标准控件的自定义控件中把常用的方法封装,比如设置字体大小: 调用自定义控件的自定义函数 还可以继续创建子蓝图控件

  4. c# 中的UserControl是什么 用户控件和自定义控件有什么区别

    用户控件是许多控件的集成 自定义控件是自己写一个控件类,或者继承已有的控件类 复合控件是封装在公共容器内的 Windows 窗体控件的集合.这种控件有时称为“用户控件”.包含的控件称为“构成控件”. ...

  5. Android利用已有控件实现自定义控件

    Android控件的基本介绍及使用自定义控件的意义         Android 本身提供了很多控件,自定义控件在android中被广泛运用,自定义控件给了我们很大的方便.比如说,一个视图为imag ...

  6. Android自定义日历控件(继承系统控件实现)

    Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...

  7. WPF之路——用户控件对比自定义控件UserControl VS CustomControl)

    将多个现有的控件组合成一个可重用的“组”. 由一个XAML文件和一个后台代码文件. 不能使用样式和模板. 继承自UserControl类. 自定义控件(扩展) 在现有的控件上进行扩展,增加一些新的属性 ...

  8. android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例

    有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果. 以下主要参考<第一行代码> 1.自定义一个标题栏: 系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在o ...

  9. 【Winform-自定义控件】自定义控件学习+一个笑脸控件例子

    1.CompositeControls组合控件:在原有控件的基础上根据需要进行组合 2.ExtendedControls 扩展控件:继承自原有控件,添加一些新的属性和方法,绘制一些新元素 当每个But ...

随机推荐

  1. Cocos2dx使用wxsqlite开源加密SQLite3数据库

    最近使用wxsqlite加密sqlite3数据库,刚开始折腾好几天,在xcode上一直编译不通过,后来在sqlite3.c找到配置,编译顺利通过,太激动了,哈哈,废话少说!总结一下android和io ...

  2. 我的常用mixin 之 lines

    /** * 最多显示 $lineCount 行 * lines * * example: * @include lines; * @include lines(3); */ @mixin lines( ...

  3. LNMP软件安装所在的目录详细

    LNMP相关软件安装目录Nginx 目录: /usr/local/nginx/MySQL 目录 : /usr/local/mysql/MySQL数据库所在目录:/usr/local/mysql/var ...

  4. 提高FOR插入数据库动作的优化代码

    await Task.Factory.StartNew(() => Parallel.ForEach(result.data.o, s => { sql = "insert in ...

  5. JDBC性能分析与优化

    JDBC性能分析与优化V1.0http://www.docin.com/p-758600080.html

  6. IT战略规划咨询

    目录 1IT战略规划微咨询简介 2IT战略的意义 3服务模式 4IT战略规划焦点问题 5IT战略规划步骤 6服务提供方微咨询网 7微咨询价值 8微咨询服务方式 9IT工作规划与IT战略规... IT战 ...

  7. 精选19款华丽的HTML5动画和实用案例

    下面是本人收集的19款超酷HTML5动画和实用案例,觉得不错,分享给大家. 1.HTML5 Canvas火焰喷射动画效果 还记得以前分享过的一款HTML5烟花动画HTML5 Canvas烟花特效,今天 ...

  8. Linux Unix 环境变量设置实例

    背景 从第一次写Hello World我们便开始接触环境变量.这最基础的系统设置是必须要掌握的,尤其在是Linux/Unix系统中.比如,哪天某个Java进程出现问题,我们想分析一下其线程堆栈,却发现 ...

  9. 设计模式——"simple Factory"

    顾名思义,工厂模式就是类似于生活中的工厂,可以生产我们想要的东西,回到代码,假如我们需要一个实例,直接从工厂中拿即可. eg:假如我们定义一个“球”类的接口 public interface Ball ...

  10. MyEclipse自动生成hibernate实体类和配置文件攻略

    步骤1:找到导航栏里面的window--showView然后输入db brower,打开数据库浏览窗口步骤2:在数据库浏览窗口里只有一个Myeclipse自带的数据库,该数据没有用,我们在空白的地方右 ...