C# .Net Treeview with multiple selection
The .Net treeview control has no built-in multiple selection. Let there be. This article about C# depicts a treeview control with multiple selection, derived from the base .Net treeview control. It supports CTRL and SHIFT combinations.
How to use itThis control is a C# control. Once compiled, it becomes a managed .Net component and behaves much like good ol' ActiveX components for VB developers. To use it in your application, you have at least two options. The first is to simply reference the TreeViewMS project given in the source project by clicking right on your current Windows Form application and choosing Add Reference, then browse to TreeViewMS.csproj. The screen capture below shows the steps :
Adding a reference to the new treeview control to your code You may also add the treeview control once for all in your Toolbox window, then simply drag&drop it onto your Form. In order to do this, show up the .Net studio Toolbox window, then right click and choose Customize toolbox, then choose the .Net Framework components tab, and browse to the compiled control : TreeViewMS.dll, as in the capture below:
Adding the new treeview control to the VisualStudio .Net toolbox Once you have added this treeview control in a form, you may start to use it like the base .Net treeview control. The addition lies in a new exposed property, SelectedNodes, which returns the collection of selected TreeNode items. If we take the demo app, which adds selected items in the list view on the right, then code goes like this : private TreeViewMS.TreeViewMS treeViewMS1; private System.Windows.Forms.ListView listView1; ... // add selected items from treeview to the listview on the right hand side foreach (TreeNode n in treeViewMS1.SelectedNodes) { listView1.Items.Add( n.Text, n.ImageIndex ); }SelectedNodes is also a read-write property. What follows is a sample code that forces the selection of given tree items : TreeNode n1 = treeViewMS1.Nodes[0].Nodes[0].Nodes[0]; // valid item from the sample tree TreeNode n2 = treeViewMS1.Nodes[0].Nodes[0].Nodes[2]; // valid item from the sample tree ArrayList coll = new ArrayList(); coll.Add(n1); coll.Add(n2); treeViewMS1.SelectedNodes = coll;
Technical detailsOf course, this control is derived from the base Treeview control :public class TreeViewMS : System.Windows.Forms.TreeView { // class members protected ArrayList m_coll; protected TreeNode m_lastNode, m_firstNode; ... /* API method */ public ArrayList SelectedNodes { get { return m_coll; } set { removePaintFromNodes(); m_coll.Clear(); m_coll = value; paintSelectedNodes(); } } }What we need to control item selection is events such like BEFORESELECT and AFTERSELECT to get the current selected node. BEFORESELECT is only useful to undo a node selection, for instance when you select twice a node with CTRL down. Because Microsoft have clearly figured out that treeviews were likely to be derived, they have overridable methods BeforeSelect(...) and AfterSelect(...) that don't interfere with the events named the same. All what we need is override the 2 methods and not forget to call the base class in the implementation : protected override void OnBeforeSelect(TreeViewCancelEventArgs e) { // e.Node is the current node exposed by the base TreeView control base.OnBeforeSelect(e); bool bControl = (ModifierKeys==Keys.Control); bool bShift = (ModifierKeys==Keys.Shift); // selecting twice the node while pressing CTRL ? if (bControl && m_coll.Contains( e.Node ) ) { // unselect it (let framework know we don't want selection this time) e.Cancel = true; // update nodes removePaintFromNodes(); m_coll.Remove( e.Node ); paintSelectedNodes(); return; } m_lastNode = e.Node; if (!bShift) m_firstNode = e.Node; // store begin of shift sequence } protected override void OnAfterSelect(TreeViewEventArgs e) { // e.Node is the current node exposed by the base TreeView control base.OnAfterSelect(e); bool bControl = (ModifierKeys==Keys.Control); bool bShift = (ModifierKeys==Keys.Shift); if (bControl) { if ( !m_coll.Contains( e.Node ) ) // new node ? { m_coll.Add( e.Node ); } else // not new, remove it from the collection { removePaintFromNodes(); m_coll.Remove( e.Node ); } paintSelectedNodes(); } else { if (bShift) { Queue myQueue = new Queue(); TreeNode uppernode = m_firstNode; TreeNode bottomnode = e.Node; // case 1 : begin and end nodes are parent bool bParent = isParent(m_firstNode, e.Node); // is m_firstNode parent (direct or not) of e.Node if (!bParent) { bParent = isParent(bottomnode, uppernode); if (bParent) // swap nodes { TreeNode t = uppernode; uppernode = bottomnode; bottomnode = t; } } if (bParent) { TreeNode n = bottomnode; while ( n != uppernode.Parent) { if ( !m_coll.Contains( n ) ) // new node ? myQueue.Enqueue( n ); n = n.Parent; } } // case 2 : nor the begin nor the end node are descendant one another else { if ( (uppernode.Parent==null && bottomnode.Parent==null) || (uppernode.Parent!=null && uppernode.Parent.Nodes.Contains( bottomnode )) ) // are they siblings ? { int nIndexUpper = uppernode.Index; int nIndexBottom = bottomnode.Index; if (nIndexBottom < nIndexUpper) // reversed? { TreeNode t = uppernode; uppernode = bottomnode; bottomnode = t; nIndexUpper = uppernode.Index; nIndexBottom = bottomnode.Index; } TreeNode n = uppernode; while (nIndexUpper <= nIndexBottom) { if ( !m_coll.Contains( n ) ) // new node ? myQueue.Enqueue( n ); n = n.NextNode; nIndexUpper++; } // end while } else { if ( !m_coll.Contains( uppernode ) ) myQueue.Enqueue( uppernode ); if ( !m_coll.Contains( bottomnode ) ) myQueue.Enqueue( bottomnode ); } } m_coll.AddRange( myQueue ); paintSelectedNodes(); m_firstNode = e.Node; // let us chain several SHIFTs if we like it } // end if m_bShift else { // in the case of a simple click, just add this item if (m_coll!=null && m_coll.Count>0) { removePaintFromNodes(); m_coll.Clear(); } m_coll.Add( e.Node ); } } } protected void paintSelectedNodes() { foreach ( TreeNode n in m_coll ) { n.BackColor = SystemColors.Highlight; n.ForeColor = SystemColors.HighlightText; } } protected void removePaintFromNodes() { if (m_coll.Count==0) return; TreeNode n0 = (TreeNode) m_coll[0]; Color back = n0.TreeView.BackColor; Color fore = n0.TreeView.ForeColor; foreach ( TreeNode n in m_coll ) { n.BackColor = back; n.ForeColor = fore; } }As you may note, SelectedNodes returns a System.Collections.ArrayList instance of underlying treeview items, not the more natural System.Windows.Forms.TreeNodeCollection instance. Why this ? in fact, that's not up to developers, the TreeNodeCollection is deliberately provided by the .Net framework with a hidden constructor, hence it is not possible to reuse it. This has raised some concern on public newsgroups but Microsoft people have not agreed to change this anytime soon.
Simpler reuseWhat if you don't want to redistribute the TreeViewMS.dll library (I admit that separate librairies are always bottlenecks) ? All you have to do is take the code for OnBeforeSelect() and OnAfterSelect() as described above and attach it to the standard treeview events.Thanks to David Sleeckx for the bug hunting.
Stephane Rodriguez-August 8, 2002. Updated August 18, 2002.
|
Home Blog |