C#派生子类控件的使用经验(C#自定义控件)

在日常的编程当中,我们为了能够重复使用某些经过设置或者是组合后的控件,如ToolStrip、Panel等容器类的控件,如果为了设计控件期间能拖放控件即见即所得的效果的话,一般人都会使用UserControl,然后在UserControl中放入ToolStrip或者是Panel等容器类控件,然后再加入我们需要使用的button、textbox、conbobox、checkbox等等,用到的时候,再把这些做好的UserControl放到相应的窗体或者其它容器中,从而达到重复使用的目的。虽然,这样做很方便,但我们的程序却多了个不必要的控件UserControl,或多或少地影响了程序的效率与资源的使用,因此,我们考虑直接使用继承容器类来实现,以减少不必要的UserControl的创建。

我们来举个ToolStrip的例子,做数据方面开发的都涉及到增删改的操作,因此,我就举个ToolStrip中放入了3个按钮,分别是“新增”、“修改”、“删除”这样一个例子。我们可以像制作UserControl那样做一次,也可以直接在窗体放入ToolStrip控件,然后操作控件增加这3个按钮,修改好各个按钮的属性,系统会自动为我们所做的每一步自动生成了代码,放在了**.Designer.cs这个文件里面。准备工作已经完成了,我们下面介绍这个派生类如何制作:

我们现在创建一个组件类:DataControl,

DataControl.cs代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace Piggy.Control.pgControl
{
    
public partial class DataControl : ToolStrip
    {
        
public DataControl()
        {
            InitializeComponent();
        }
    }
}

这里必须注意的是:构造函数中必须有InitializeComponent();这个过程,否则,你在窗体中加入这个控件如同加入父类一样,没有任何效果的。

下面,我们只需要把窗体或者是UserControl的**.Designer.cs中的涉及到这3个按钮的代码拷贝下来,粘贴到DataControl.Designer.cs的代码中去,

DataControl.Designer.cs的代码如:

代码
namespace Piggy.Control.pgControl
{
    
partial class DataControl
    {
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.IContainer components = null;

        
/// <summary> 
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            
if (disposing && (components != null))
            {
                components.Dispose();
            }
            
base.Dispose(disposing);
        }

        
#region 组件设计器生成的代码

        
/// <summary>
        
/// 设计器支持所需的方法 - 不要
        
/// 使用代码编辑器修改此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {
            
this.tsBtnNew = new System.Windows.Forms.ToolStripButton();
            
this.tsBtnEdit = new System.Windows.Forms.ToolStripButton();
            
this.tsBtnDelete = new System.Windows.Forms.ToolStripButton();
            
this.SuspendLayout();
            
// 
            
// tsBtnNew
            
// 
            this.tsBtnNew.Image = global::Piggy.Control.IcoResource.new16;
            
this.tsBtnNew.ImageTransparentColor = System.Drawing.Color.Magenta;
            
this.tsBtnNew.Name = "tsBtnNew";
            
this.tsBtnNew.Size = new System.Drawing.Size(4833);
            
this.tsBtnNew.Text = "新增";
            
this.tsBtnNew.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            
// 
            
// tsBtnEdit
            
// 
            this.tsBtnEdit.Image = global::Piggy.Control.IcoResource.edit16;
            
this.tsBtnEdit.ImageTransparentColor = System.Drawing.Color.Magenta;
            
this.tsBtnEdit.Name = "tsBtnEdit";
            
this.tsBtnEdit.Size = new System.Drawing.Size(4833);
            
this.tsBtnEdit.Text = "修改";
            
this.tsBtnEdit.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            
// 
            
// tsBtnDelete
            
// 
            this.tsBtnDelete.Image = global::Piggy.Control.IcoResource.delete16;
            
this.tsBtnDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
            
this.tsBtnDelete.Name = "tsBtnDelete";
            
this.tsBtnDelete.Size = new System.Drawing.Size(4833);
            
this.tsBtnDelete.Text = "删除";
            
this.tsBtnDelete.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;
            
// 
            
// DataControl
            
// 
            this.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            
this.tsBtnNew,
            
this.tsBtnEdit,
            
this.tsBtnDelete
});
            
this.Size = new System.Drawing.Size(10030);
            
this.ResumeLayout(false);

        }

        
#endregion
        
private System.Windows.Forms.ToolStripButton tsBtnNew;
        
private System.Windows.Forms.ToolStripButton tsBtnEdit;
        
private System.Windows.Forms.ToolStripButton tsBtnDelete;
    }
}

这样,这个子类控件就算创建成功了。编译通过后,我们把这个控件直接放在窗体或者其它容器中,就能看到这3个按钮已经是创建了出来,并显示了出来的了。前面那红色字的注意是这文章值得关注的重点,大家跟踪下代码就知道,如果不在构造函数那里执行InitializeComponent();这个过程,系统根本就不会自动去执行这个方法的,一直都以为这个方法会被基类自动调用。

一直都不知道如何能够在添加自定义控件的时候,能在设计的地方看到控件制作的效果。如果有人知道的,请告诉我,谢谢。

 原创作品出自努力偷懒,转载请说明文章出处http://www.cnblogs.com/kfarvid/

原文地址:https://www.cnblogs.com/kfarvid/p/1892205.html