首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 組件編程(六):Component Designer 基礎與進階

     編輯:關於.NET

Designer 為 Component 設計時服務,可修改設計時行為、提供自訂服務。繼承 ComponentDesigner(針對 Component)或 ControlDesigner(針對 Control)即可自訂。

主要功能

  • 右鍵選單命令:覆寫 Verbs 屬性回傳 DesignerVerbCollection。
  • 雙擊預設動作:覆寫 DoDefaultAction()。
  • 工具箱拖放初始化:覆寫 InitializeNewComponent() 設定預設屬性值。
  • 屬性過濾:覆寫 PostFilterProperties() 移除/新增屬性。

完整範例

[DefaultEvent("CustomerLogin")]
[Designer(typeof(Class1Designer), typeof(IDesigner)), Editor(typeof(Class1Editor), typeof(ComponentEditor))]
public class Customer : Component {
    public string ParentComponentName { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public event EventHandler CustomerLogin;
    public event EventHandler CustomerLogout;
}

public class Class1Designer : ComponentDesigner {
    public Class1Designer() {
        this.Verbs.Add(new DesignerVerb("Click Me", OnChoseMe));
    }
    void OnChoseMe(object s, EventArgs e) { MessageBox.Show("You clicked."); }
    public override void DoDefaultAction() { MessageBox.Show("You double clicked."); }
    public override void InitializeNewComponent(IDictionary defaultValues) {
        string parentName = ((Component)this.ParentComponent).Site.Name;
        ((Customer)this.Component).ParentComponentName = parentName;
        ((Customer)this.Component).Age = 18;
    }
    protected override void PostFilterProperties(IDictionary properties) {
        properties.Remove("Address"); // 移除 Address 屬性
    }
    public override void OnSetComponentDefaults() {
        base.OnSetComponentDefaults();
        ((Customer)this.Component).Age = 18; // 過時方法,保留相容
    }
}

效果:右鍵選單顯示自訂命令、雙擊觸發訊息、拖放自動設定父容器名稱與預設年齡、屬性視窗隱藏 Address。
右鍵選單 屬性視窗 拖放初始化

隨文原始碼:https://www.bianceng.net/dotnet/201212/664.htm

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved