Visual Studio 2005 ToolBox Clone

Introduction

This control is a very simple VisualStudio ToolBox control clone. The control itself is just a owner drawn TreeView. Because it is based on a standard treeview it supports only one style of the Visual Studio Toolbox - the list style.

Background

Many weeks ago i needed a simple control that looks like the ToolBox in VisualStudio 2005. There're some control on codeproject doing stuff like this. But all this controls are too oversized for me. So i wrote my own one...

To create the look and feel of the toolbox i have just overridden the OnDrawNode method. Depending on the node level the OnDrawNode method will call the method for root items or sub items. As you can see, there're only two levels possible in this control. An item on level 0 will be a root item and drawn as the darker header items. All subitem (level > 0) will be drawn on level 1 as sub item.

Adding advanced tool tips to the toolbox was a little tricky, because the standard treeview does not support such tool tips. First i had to disable the standard tool tip functionality of the treeview like this way:

Collapse
private const int TVS_NOTOOLTIPS = 0x80;

/// <summary>
/// Disables the tooltip activity for the treenodes.
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams p = base.CreateParams;
p.Style = p.Style | TVS_NOTOOLTIPS;
return p;
}
}

After that, i have implemented a tool tip control which supports the advanced look and feel of the modern too tips (also used in the original Visual Studio ToolBox).

Using the code

The handling of this control is quite easy. If you ever have worked with the .Net TreeView control you should not have any problems with it. But you have to set some properties at this time to let the control look like the Visual Studio ToolBox:

* Set the DrawMode to OwnerDrawAll
* Set the FullRowSelect to true
* Set the HideSelection to false
* Set the HotTracking to true
* Set the ItemHeight to 20
* Add an ImageList

Adding new Groups:

Collapse
// To support the custom tool tips we
// have to add an enhanced node type to the toolbox.
ToolBox.VSTreeNode newGroup = new ToolBox.VSTreeNode();

newGroup.Text = String.Format("Sample Node {0}", toolBox1.Nodes.Count + 1);

toolBox1.Nodes.Add(newGroup);

Adding new items to a group:

Collapse
ToolBox.VSTreeNode newSubItem = new ToolBox.VSTreeNode();

newSubItem.Text = String.Format("Sample SubItem {0}",
toolBox1.SelectedNode.Nodes.Count + 1);

// Assuming, that a image list is set.
newSubItem.ImageIndex = 0;
newSubItem.ToolTipCaption = "Look atg this!";
newSubItem.ToolTipText = "This is an example ToolTip.";

// It's also possible to add a context menu to a node (root or subitem)
newSubItem.ContextMenuStrip = cmsExample;

// Add the new subitem to the toolbox.

toolBox1.SelectedNode.Nodes.Add(newSubItem);
->Read More...

0 Comments:

Đăng nhận xét