Wednesday, May 27, 2009

Adding a TrackBar to a Windows Forms StatusStrip

StatusStrip is a ToolStrip, one of the oh-so-versatile WinForms 2.0 innovations introduced with the .Net 2.0 Framework. I wanted to add a TrackBar to the StatusStrip which isn't supported out of the box but, fortunately, WinForms2.0 ships with a ToolStripControlHost that can be used to wrap any Windows Forms control for embedding within a ToolStrip.

The basic idea is to derive from ToolStripControlHost (e.g., ToolStripTrackBar : ToolStripControlHost), expose the properties and values of the underlying control, pass through events via OnSubscribeControlEvents and OnUnsubscribeControlEvents.

One stumbling block was designer support; since I was setting up handling of the scroll event in the control's Load event, the WinForms designer would crash while trying to execute it when scrolled in design mode. A way around this was to detect when the ToolStripTrackBar is running in design mode (via Control.Site != null !Control.Site.DesignMode)

No comments :

Post a Comment