« Accessing Embedded Resources using GetManifestResourceStream | Main | Windows NT: The Godzilla of Operating Systems »

Using ToolStripControlHost With a Trackbar

I'm currently developing an application that displays images (bmp, jpg, etc) in a Windows Forms application. The application can scale the image within a panel, making it fit, or allowing an interactive scale. To enhance this functionality, I wanted to include a slider Trackbar control to my toolstrip. The end result needed to look like this:

trackbar on toolstrip

Fortunately, this is easily accomplished with .NET 2.0, by using the ToolStripControlHost class to wrap controls for the ToolStrip. I was even able to find code that specifically added the Trackbar on WindowsForms.net. All I had to do was place this code inside a file called ToolStripTrackBar.cs (see the link to this below), add a few using clauses to bring in the required assemblies, and it compiled just fine. Now what? I spent a good period of time just trying to find TrackBar in the Visual Studio Toolbox. At first I thought it might show up where the user controls are located, under the first control group with the name of your program. Nothing new there. Nada for the "All Windows Forms" control group. I couldn't find it anywhere. Compiled again, exited and restarted Visual Studio. Nada. I decided to give up and decided to add some buttons to my toolstrip. Voila! On the bottom of the menu that allows you to add toolstrip items, the Trackbar item had wonderfully appeared!

trackbar on toolstrip menu

It's all very easy and automatic, however none of the references to this specific example told me where to find the Trackbar. The benefits of having the Trackbar in Visual Studio designer are great, as you can visually position and resize the control. However, the Properties settings for the trackbar object, embedded in the ToolStripControlHost, were not persistent. I had to write a method to intialize the trackbar object:

private void InitTrackBar()
{
    zoomToolStripTrackBar.TrackBar.TickStyle = 
        TickStyle.None;
    zoomToolStripTrackBar.TrackBar.BackColor = 
        this.toolStrip1.BackColor;
    zoomToolStripTrackBar.TrackBar.Scroll += 
        new EventHandler(this.scrollZoom_Scroll);
    zoomToolStripTrackBar.TrackBar.Value = 5;
}

That was problem #1. Problem #2 was that while the ToolStrip was set to ManagerRenderMode, this property does not automatically extend to the ToolStripControlHost. If you look at my first picture, you'll notice that the TrackBar is grey while the rest of the ToolStrip is blue. If I figure out the reason for this, I'll record it here.

Link:
ToolStripTrackBar.cs

Comments (3)

Jim:

This is exactly what I am looking for! I am new at this. Where do you put the ToolStripTrackBar.cs file?

Jim:

Nevermind. I figured it out!
Project > Add Existing Item
Then build (or debug) once does the trick.

Thanks again for the lead.

It's now available in the Crystal Toolkit. For the latest version, get it on the Downloads page:

http://www.attilan.com/downloads/

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on September 1, 2006 10:53 PM.

The previous post in this blog was Accessing Embedded Resources using GetManifestResourceStream.

The next post in this blog is Windows NT: The Godzilla of Operating Systems.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by Movable Type 4.1
Hosted by LivingDot