如何去掉TabControl控件默认添加的TabPage

把TabControl添加到设计器的时候,默认会添加两个TabPage,当继承TabControl自定义控件的时候,这两个默认的TabPage常常会制造一些麻烦,今天我来介绍一种方法来去掉这两个默认的TabPage:
实际上思路比较简单,主要是通过ToolboxItem特性提供自定义的ToolboxItem类来修改工具箱中的控件的初始化工程,只需要继承ToolboxItem类,重写CreateComponentsCore方法就可以实现了:
    [ToolboxItem(typeof(DemoToolboxItem))]
    public class MyTabControl : TabControl
    {
    }


    [Serializable]  //ToolboxItem必须是可序列化的
    class DemoToolboxItem : ToolboxItem
    {
        // The add components dialog in VS looks for a public
        // ctor that takes a type.
        public DemoToolboxItem(Type toolType)
            : base(toolType)
        {
        }

        // And you must provide this special constructor for serialization.
        // If you add additional data to MyToolboxItem that you
        // want to serialize, you may override Deserialize and
        // Serialize methods to add that data.  
        DemoToolboxItem(SerializationInfo info, StreamingContext context)
        {
            Deserialize(info, context);
        }

        // This implementation sets the new control's Text and
        // AutoSize properties.
        protected override IComponent[] CreateComponentsCore(
            IDesignerHost host,
            IDictionary defaultValues)
        {

            IComponent[] comps = base.CreateComponentsCore(host, defaultValues);

            
            MessageBox.Show(((MyTabControl)comps[0]).TabPages.Count.ToString());
            ((MyTabControl)comps[0]).TabPages.RemoveAt(0);//去掉默认添加的TabPage
            ((MyTabControl)comps[0]).TabPages.RemoveAt(0);
            return comps;
        }
    }
当然,如果愿意的话,也可以自己在CreateComponentsCore中添加自定义的TabPage来使我们的TabControl更加的有实用价值!

转载于:https://www.cnblogs.com/xuefeng1982/archive/2010/01/07/1641482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值