首页 技术 正文
技术 2022年11月11日
0 收藏 911 点赞 2,916 浏览 3333 个字

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://msdn.microsoft.com/zh-cn/library/5h0k2e6x(v=vs.80).aspx

http://blog.csdn.net/yysyangyangyangshan/article/details/7078471程序员的网店:http://shop108042866.taobao.com

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289