向ToolStrip、MenuStrip、StatusStrip中添加自定义控件
2012-06-13 21:26
691 查看
新版framework引入了ToolStrip(代替ToolBar)、MenuStrip(代替MenuBar)、StatusStrip(代替StatusBar)这些新控件,虽然旧控件还被支持,但也只是出于兼容性的考虑。
新事物多半会伴随着新问题:这些XxxStrip提供了能够了ToolStripButton、ToolStripLabel、ToolStripDropDownItem等子项,使用它们可以满足大部分的功能需求,剩下的小部分可能需要通过自定义控件来实现了。想要将自定义控件加入到这些XxxStrip中,使用老办法显示是行不通了,下面就进入正题吧。
为了满足上述要求,framework提供了ToolStripControlHost类,它可以承载其他控件,包括两种方式的自定义控件:
1. 使用从 Control 派生的类构造一个 ToolStripControlHost。 要对寄宿的控件和属性进行完全访问,必须重新将 Control 属性强制转换为它所代表的实际类。
2. 扩展 ToolStripControlHost,然后在继承类的默认构造函数中调用基类构造函数,以传递从 Control 派生的类。 此选项允许您对公共控件方法和属性进行包装,以使 ToolStrip 中的访问更加容易。
上面这两条听起来有点晦涩(嗯,很正常,因为这是MSDN的原文 -_-!),那就让我们来看代码吧。
下面是实现一个可以加入到XxxStrip中的自定义月份牌,需要注意的是,在该类的构造函数中调用了ToolStripControlHost构造,并传入了framework自带的MonthCalendar控件;在重载的OnSubscribeControlEvents函数中,对MonthCalendar控件的事件进行处理。
//Declare a class that inherits from ToolStripControlHost.
public class ToolStripMonthCalendar : ToolStripControlHost
{
// Call the base constructor passing in a MonthCalendar instance.
public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
public MonthCalendar MonthCalendarControl
{
get
{
return Control as MonthCalendar;
}
}
// Expose the MonthCalendar.FirstDayOfWeek as a property.
public Day FirstDayOfWeek
{
get
{
return MonthCalendarControl.FirstDayOfWeek;
}
set { value = MonthCalendarControl.FirstDayOfWeek; }
}
// Expose the AddBoldedDate method.
public void AddBoldedDate(DateTime dateToBold)
{
MonthCalendarControl.AddBoldedDate(dateToBold);
}
// Subscribe and unsubscribe the control events you wish to expose.
protected override void OnSubscribeControlEvents(Control c)
{
// Call the base so the base events are connected.
base.OnSubscribeControlEvents(c);
// Cast the control to a MonthCalendar control.
MonthCalendar monthCalendarControl = (MonthCalendar) c;
// Add the event.
monthCalendarControl.DateChanged +=
new DateRangeEventHandler(OnDateChanged);
}
protected override void OnUnsubscribeControlEvents(Control c)
{
// Call the base method so the basic events are unsubscribed.
base.OnUnsubscribeControlEvents(c);
// Cast the control to a MonthCalendar control.
MonthCalendar monthCalendarControl = (MonthCalendar) c;
// Remove the event.
monthCalendarControl.DateChanged -=
new DateRangeEventHandler(OnDateChanged);
}
// Declare the DateChanged event.
public event DateRangeEventHandler DateChanged;
// Raise the DateChanged event.
private void OnDateChanged(object sender, DateRangeEventArgs e)
{
if (DateChanged != null)
{
DateChanged(this, e);
}
}
}
MSDN到这里就结束了,但在使用过程中,我发现了另一个小问题:用户控件(通过各种已有控件进行组装而成,而不是继承某个控件再对其扩展)中使用Dock属性引发的问题,go on.
假设用户控件中有几个控件,其中一个控件A的Dock属性设置为Fill,那么运行程序后,界面上将看不到控件A,解决办法是:将用户控件中的所有控件都放入一个Panel控件B中(不能设置B的Dock属性为Fill,这将使整个用户控件消失,其实ToolStripControlHost在被加到XxxStrip中时,会自动Fill),然后将B的大小设置的与用户控件相同,最后设置B的Anchor属性为上下左右即可,在B中的控件无论怎样设置Dock属性都不会有问题了。
注:以上结论在StatusStrip中实验通过。
最后,good luck for everyone!
新事物多半会伴随着新问题:这些XxxStrip提供了能够了ToolStripButton、ToolStripLabel、ToolStripDropDownItem等子项,使用它们可以满足大部分的功能需求,剩下的小部分可能需要通过自定义控件来实现了。想要将自定义控件加入到这些XxxStrip中,使用老办法显示是行不通了,下面就进入正题吧。
为了满足上述要求,framework提供了ToolStripControlHost类,它可以承载其他控件,包括两种方式的自定义控件:
1. 使用从 Control 派生的类构造一个 ToolStripControlHost。 要对寄宿的控件和属性进行完全访问,必须重新将 Control 属性强制转换为它所代表的实际类。
2. 扩展 ToolStripControlHost,然后在继承类的默认构造函数中调用基类构造函数,以传递从 Control 派生的类。 此选项允许您对公共控件方法和属性进行包装,以使 ToolStrip 中的访问更加容易。
上面这两条听起来有点晦涩(嗯,很正常,因为这是MSDN的原文 -_-!),那就让我们来看代码吧。
下面是实现一个可以加入到XxxStrip中的自定义月份牌,需要注意的是,在该类的构造函数中调用了ToolStripControlHost构造,并传入了framework自带的MonthCalendar控件;在重载的OnSubscribeControlEvents函数中,对MonthCalendar控件的事件进行处理。
//Declare a class that inherits from ToolStripControlHost.
public class ToolStripMonthCalendar : ToolStripControlHost
{
// Call the base constructor passing in a MonthCalendar instance.
public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
public MonthCalendar MonthCalendarControl
{
get
{
return Control as MonthCalendar;
}
}
// Expose the MonthCalendar.FirstDayOfWeek as a property.
public Day FirstDayOfWeek
{
get
{
return MonthCalendarControl.FirstDayOfWeek;
}
set { value = MonthCalendarControl.FirstDayOfWeek; }
}
// Expose the AddBoldedDate method.
public void AddBoldedDate(DateTime dateToBold)
{
MonthCalendarControl.AddBoldedDate(dateToBold);
}
// Subscribe and unsubscribe the control events you wish to expose.
protected override void OnSubscribeControlEvents(Control c)
{
// Call the base so the base events are connected.
base.OnSubscribeControlEvents(c);
// Cast the control to a MonthCalendar control.
MonthCalendar monthCalendarControl = (MonthCalendar) c;
// Add the event.
monthCalendarControl.DateChanged +=
new DateRangeEventHandler(OnDateChanged);
}
protected override void OnUnsubscribeControlEvents(Control c)
{
// Call the base method so the basic events are unsubscribed.
base.OnUnsubscribeControlEvents(c);
// Cast the control to a MonthCalendar control.
MonthCalendar monthCalendarControl = (MonthCalendar) c;
// Remove the event.
monthCalendarControl.DateChanged -=
new DateRangeEventHandler(OnDateChanged);
}
// Declare the DateChanged event.
public event DateRangeEventHandler DateChanged;
// Raise the DateChanged event.
private void OnDateChanged(object sender, DateRangeEventArgs e)
{
if (DateChanged != null)
{
DateChanged(this, e);
}
}
}
MSDN到这里就结束了,但在使用过程中,我发现了另一个小问题:用户控件(通过各种已有控件进行组装而成,而不是继承某个控件再对其扩展)中使用Dock属性引发的问题,go on.
假设用户控件中有几个控件,其中一个控件A的Dock属性设置为Fill,那么运行程序后,界面上将看不到控件A,解决办法是:将用户控件中的所有控件都放入一个Panel控件B中(不能设置B的Dock属性为Fill,这将使整个用户控件消失,其实ToolStripControlHost在被加到XxxStrip中时,会自动Fill),然后将B的大小设置的与用户控件相同,最后设置B的Anchor属性为上下左右即可,在B中的控件无论怎样设置Dock属性都不会有问题了。
注:以上结论在StatusStrip中实验通过。
最后,good luck for everyone!
相关文章推荐
- C#, VS2010, Tablelayout的Dock = Fill属性无效,被statusStrip或toolStrip遮住
- StatusStrip如何添加分割栏?
- C#将StatusStrip控件中的信息添加…
- C#中toolStrip或statusStrip遮挡了SplitContainer怎么办?
- ToolStrip动态添加自定义控件
- C#添加toolstripstatuslabel
- StatusStrip和ToolStrip关联控件丢失问题
- N2CMS 实践之路 Tip.3 自定义控件如何添加
- ContextMenuStrip 中添加子菜单
- 在 SystemUI 的 Statusbar 中 添加 ethernet status icon
- Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件
- c# statusStrip控件(转)
- 疑问:AddWebPart 不能添加动态加载的自定义控件吗?
- 动态添加自定义控件:即按一下Botton添加一个UseControl,再按一下又添加一个
- asp.net中添加自定义控件的方法
- StatusStrip 控件
- WIN8 自定义控件添加到页面XAML报错的解决方法
- 给自定义控件添加属性
- 自定义控件三部曲之绘图篇(十七)——为Bitmap添加阴影并封装控件
- ViewPager 详解(三)---PagerTabStrip与PagerTitleStrip添加标题栏的异同