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

移除 TabControl 預設新增的 TabPage 技巧

     編輯:關於.NET

問題背景

將 TabControl 拖入設計器時,預設會加入兩個 TabPage。若繼承 TabControl 開發自訂控制項,這兩個預設頁籤常造成干擾。本文介紹透過自訂 ToolboxItem 移除預設 TabPage 的方法。

實作原理

利用 [ToolboxItem] 屬性指定自訂 ToolboxItem 類別,覆寫 CreateComponentsCore 方法,在元件建立後移除預設頁籤。

完整程式碼

[ToolboxItem(typeof(DemoToolboxItem))]
public class MyTabControl : TabControl
{
}

[Serializable]
class DemoToolboxItem : ToolboxItem
{
    public DemoToolboxItem(Type toolType) : base(toolType) { }

    protected DemoToolboxItem(SerializationInfo info, StreamingContext context)
    {
        Deserialize(info, context);
    }

    protected override IComponent[] CreateComponentsCore(IDesignerHost host, IDictionary defaultValues)
    {
        IComponent[] comps = base.CreateComponentsCore(host, defaultValues);
        // 移除兩個預設 TabPage
        ((MyTabControl)comps[0]).TabPages.RemoveAt(0);
        ((MyTabControl)comps[0]).TabPages.RemoveAt(0);
        return comps;
    }
}

延伸應用

在 CreateComponentsCore 中亦可自行加入自訂 TabPage,賦予控制項更多實用預設值。

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