用Visual studio 创建自定义控件

想了解一下自定义控件的知识,不过,在自定义控件在工具条上显示,花了些时间。


最后是出来的,但这里不想仔细找原因了,大至总结几个可能的点:


1. 新建windows forms control library工程

 


 

2. vs设置如下:



    工具-》选项-》Windows窗体设计器-》常规-》工具箱-》AutoToolBoxPopulate 改为TRUE

 

vs 2010所在位置,这个图有错误,是toolbax下面那个。

 

 

 

 

2. 检查项目的.NET Framework项目的版本是否和开发控件的.NET版本一致

寻找方法,喜爱敏捷,实践方法,不断总结,坚持到底,终将成功。

3. 基类最好用完整空间名称
public partial class UserControl1 : System.Windows.Forms.Button

.designer.cs 最好删除

4. 添加这两段:

 

  • Add the following code to the Declarations section:
    using System.ComponentModel.Design;
  • Apply the System.ComponentModel.DesignerAttribute attribute to the control as follows:
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))] 
    public class UserControl1 : System.Windows.Forms.UserControl
    {
    
          ...
    
    }
    5. 要重新编译。

    6.最后自动就会出来。

  •  


  • ===================

    [
        Category("Gradient"),
        Description("First Gradient Color")
    ]
    ========================

    一些链接

    https://workspaces.codeproject.com/haoyujie/writing-your-custom-control-step-by-step

    http://support.microsoft.com/default.aspx?scid=kb;en-us;813450

    http://www.codeproject.com/Articles/837/Your-first-C-control

    http://www.cnblogs.com/jerryjaord/archive/2011/05/09/2040805.html

    =====================

    Overview


    By default, a UserControl object can act as a control container only when you create the control. To make aUserControl host a constituent control after you put the UserControl on a Windows Form, you must change the default designer of theUserControl. To implement design-time services for a component, use theDesignerAttribute class of the System.ComponentModel namespace. TheDesignerAttribute comes before the class declaration. Initialize theDesignerAttribute by passing the designerTypeName and thedesignerBaseType parameters.

    designerTypeName is the fully qualified name of the designer type that provides design-time services. Pass the combination of theSystem.Windows.Forms.Design.ParentControlDesigner and the System.Design for the designerTypeName parameter. The ParentControlDesigner class extends design-time behavior for a UserControl.

    designerBaseType is the name of the base class for the designer. The class that is used for the design-time services must implement the IDesigner interface.


    Create the UserControl as a Design-Time Control Container

    1. Create a new Visual C# Windows Control Libraryproject. To do this, follow these steps:
      1. Start Visual Studio.
      2. On the File menu, point toNew, and then click Project.
      3. Under Project Types, clickVisual C# , and then click Windows Forms Control Library under Templates.

        Note In Visual Studio 2003, click Visual C# Projects underProject Types and then click Windows Control Library under Templates.
    2. Name the projectContainerUserControl. By default,UserControl1.cs is created.
    3. In Solution Explorer, right-clickUserControl1.cs, and then clickViewCode.
    4. Add the following code to the Declarations section:
      using System.ComponentModel.Design;
    5. Apply the System.ComponentModel.DesignerAttribute attribute to the control as follows:
      [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))] 
      public class UserControl1 : System.Windows.Forms.UserControl
      {
      
            ...
      
      }
      
    6. On the Build menu, click BuildSolution.


    Test the UserControl

    1. Create a new Visual C# project. To do this, follow thesesteps:
      1. Start Visual Studio.
      2. On the File menu, point toNew, and then click Project.
      3. Under Project Types, clickVisual C#, and then click Windows Forms Application under Templates. By default,Form1.cs is created.

        Note In Visual Studio 2003, click Visual C# Projects underProject Types, and then click Windows Control Library under Templates.
    2. Add the UserControl1 control to the toolbox.
      1. On the Tools menu, clickChoose Toolbox Items.
      2. On the .NET Framework Components tab, clickBrowse.
      3. In the Open File box, locate the DLL that was built when you created theUserControl control.
    3. Drag UserControl1 from the toolbox (underWindows Forms) toForm1.cs.
    4. Drag a Button control from the toolbox toUserControl1.
    5. Notice that the UserControl1 behaves ascontrol container for theButton control.

    Collapse imageREFERENCES

    For more information, see the following Microsoft Web sites:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值