Non-standard Toolstrip Controls

I recently had to add a .Net toolstrip I implemented a few years ago to a ToolStripContainer so it could be moved and docked, the ToolStrip had a horizontal scrollbar “placed” on it, i.e. a normal scrollbar control that has a fixed position on a certain place on the ToolStrip. This was done because the built in controls available on a ToolStrip didnt include a scrollbar.

However,  the ability to move the ToolStrip meant that the scrollbar was left at the fixed location, luckily before I tried to get the scrollbar to  move with the ToolStrip I discovered the ToolStripControlHost class. The MSDN documentation succinctly describes as “ToolStripControlHost is designed to enable hosting of arbitrary Windows Forms controls”.

Brilliant. This has allowed the fixed position scrollbar to be hosted on the ToolStrip, which then handles all the movement etc. The only downside is the hosted items have to be constructed programmatically rather than using the UI designer, this is shown below:

//Create the scrollbar
 HScrollBar scrollBar = new HScrollBar();
//Add EventHandler and modify any properties etc...
 scrollBar.Scroll += new ScrollEventHandler(ScrollBar_Scroll);
//Configure the ToolStripControlHost to host the scrollbar
 ToolStripControlHost hostedItem = new ToolStripControlHost(scrollBar);
//Add the hosted item at the required position on the ToolStrip.
 int positionOnToolStrip = 0;
 toolStrip.Items.Insert(positionOnToolStrip, hostedItem);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s