我之前用VFP的,可以很隨意建立自己的控件,例如建立一個label控件,定義好字型、字體大小、顏色等屬性,在開發系統的時候就可以調用此label控件,不用每次去設置屬性,在C#中不知如何設置,達到該功能,請大俠指點。

解决方案 »

  1.   

    在工程上点右键 Add->User Control
      

  2.   

    给你个最简单的BUTTON的例子
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing; namespace MaryKay.Library
    {
    /// <summary>
    /// </summary>
    [ToolboxData("<{0}:TButton runat=server></{0}:TButton>")]
    [ToolboxBitmap(typeof(TButton),"BackLink.bmp")]
    public class TButton : Button
    {
    public TButton()
    {
    this.Text = "TButton";
    this.ToolTip = "Click to go to the previous page";
                                //.................
                              
    }
    }
    }
      

  3.   

    如果想写属性参照,类似:
    [CategoryAttribute("自定义属性")]     
    [DescriptionAttribute("源列表框标题")]
    public string CaptionSource
    {
    get
    {
    return this._strCaptionSource;
    }
    set
    {
    this._strCaptionSource = value;
    this.lblSource.Text = this._strCaptionSource;
    }
    }
      

  4.   

    事件的写法:[CategoryAttribute("自定义事件")]
    [DescriptionAttribute("为ListBox作数据绑定时发生的事件(暂停用)")]
    public event EventHandler SetListSourceEvent;protected void RaiseSetListSourceEvent()
    {
    if (SetListSourceEvent!=null)
    SetListSourceEvent(this,new EventArgs());
    }
      

  5.   

    1、扩展一个控件,直接从该控件继承
    2、从头开发一个控件,从Control继承或者从UserControl继承
    3、如果你不嫌累,还可以从根类继承
      

  6.   

    按Maeosx的方法在工程上点右键 Add->User Control,可以新建控件,但在设计过程中看不到效果,即不能实时看到更改属性后的效果,也不能把多个控件组合成一个控件。另外lee_cl提到可以和其他控件一样拖放使用,不知怎么设置,请指教。
      

  7.   

    请问 lee_cl(豆腐·BombEr) 你有这方面例子吗?
    如果有,mail一份给我 [email protected] 谢啦