Introduction
This control extends the Treeview of .NET in a very simply but effective manner. There are just a few modifications that provide multiselection, the same way, the Visual Studio 2005 does in its Project-Explorer.
Using the code
Copy the source treeview.cs into your project and use it instead of the original treeview that comes with .NET. All behaviour of the original control remains.
You can use the shift and control keys to select multiple items by keyboard and mouse.
New Property: MultiSelect
There is a new property MultiSelect, that you can use to switch multiselection on and off.
New Property: SelectedNodes
There is another property SelectedNodes, that lets you retrieve or set the multiselection. The original property SelectedNode is working as well, but it hold only the last selected node.
Full Row Selection
I also implemented a full-row-selection like in Visual Studio 2005. If you don't like that, remove the out crossed code.
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
TreeViewHitTestInfo treeViewHitTestInfo = null;
switch (e.Button)
{
case MouseButtons.Right:
treeViewHitTestInfo = this.HitTest(e.Location);
if (treeViewHitTestInfo.Location == TreeViewHitTestLocations.Label ||
treeViewHitTestInfo.Location == TreeViewHitTestLocations.RightOfLabel)
{
if (!this.selectedNodes.Contains(treeViewHitTestInfo.Node))
{
this.ClearPreviousSelection();
this.SelectedNode = treeViewHitTestInfo.Node;
}
}
break;
case MouseButtons.Left:
treeViewHitTestInfo = this.HitTest(e.Location);
if (treeViewHitTestInfo.Location == TreeViewHitTestLocations.RightOfLabel)
{
this.SelectedNode = treeViewHitTestInfo.Node;
}
break;
}
}
ContextMenu Click
As you may see in the code above, the right-click does a selection, if an unselected node is hit. This is very important, because the original treeview resets the selection after the click and so the context-menu may show wrong items.
Points of Interest
I've tested different multiselection tree controls and every time I noticed, they was made of a lot of code. Often the functionality was overloaded with nice looking things but the keyfunctionality did not work with every users input. Now I did it myself and I realized: Keep it simple, as much as you can. Multiselection can easily get very complex.
History
I would be grateful for any suggestions.
Greatings,
Jean Allisat