如何去掉TabControl控件默认添加的TabPage
2010-08-23 20:58
501 查看
把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更加的有实用价值!
实际上思路比较简单,主要是通过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更加的有实用价值!
相关文章推荐
- 如何去掉TabControl控件默认添加的TabPage
- 如何把tabControl 的tabPage控件的标题不显示,只显示矩形框的内容
- 如何设置tabcontrol控件的tabPage的text内容显示成竖着的字体
- 如何给C# 中的tabControl 控件添加图片
- C#用TabControl控件如何使某个TabPage禁用
- C# TabControl控件中TabPage选项卡选择时触发事件
- C#重绘TabControl的Tabpage标签,添加图片及关闭按钮
- this.PreviousPage.FindControl遇到MasterPage,如何查找控件?
- C# WinForm编程TabControl控件的标签TabPage怎么做成图片
- C#重绘TabControl的Tabpage标签,添加图片及关闭按钮
- 如何让tabControl中的tabpage不可用或者不显示
- Flex中如何去掉Tree控件默认文件夹图标、叶子节点图标
- 对tabcontrol控件增强,添加关闭按钮功能、呼吸灯标签闪烁功能、类QQ消息数量标签提示TIP
- 给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件
- winform如何用tabControl控件实现纵向选项卡效果?
- c#添加事件,以xtraTabPage控件添加MouseWheel事件为例,实现图像的放大缩小
- TabControl控件和TabPage
- Flex中如何去掉Tree控件默认文件夹图标、叶子节点图标
- WIN SDK 添加Tab控件 List Control with CheckBox
- MFC入门学习之控件(3)标签控件Tab Control的添加和使用