Home
Manage Your Code
Snippet: Using TreeView as menu in Windows Forms (C#)
Title: Using TreeView as menu in Windows Forms Language: C#
Description: Using TreeView as menu in Windows Forms with SplitContainer and UserControls. This snippet has four functions: treeView1_AfterSelect, findSelectedNode, showUserControl and refreshPanel. Views: 206
Author: Snorre Gartland Date Added: 10/8/2007
Copy Code  
1    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
2        {
3            findSelectedNode(splitContainer1, e);
4        }
5
6        /// <summary>
7        /// Finds the selected node.
8        /// How to use:
9        /// findSelectedNode(splitContainer1, e);
10        /// </summary>
11        /// <param name="mySplitContainer">Name of SplitContainer</param>
12        /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
13        private static void findSelectedNode(SplitContainer mySplitContainer, TreeViewEventArgs e)
14        {
15            refreshPanel(mySplitContainer);
16
17            // e.Node.Name holds the name of the node selected. The name is defined by editing the TreeView control.
18            switch (e.Node.Name)
19            {
20                case "uclOne": //uclOne is the name of the node defined in treeView1
21                    {
22                        //UserControl1 is the name of my added UserControl
23            UserControl1 usrCtrl = new UserControl1();
24                        showUserControl(mySplitContainer, usrCtrl);
25                    }
26                    break;
27                case "uclTwo": //uclTwo is the name of the node defined in treeView1
28                    {
29                        //UserControl2 is the name of my added UserControl
30                        UserControl2 usrCtrl = new UserControl2();
31                        showUserControl(mySplitContainer, usrCtrl);
32                    }
33                    break;
34                default: //uclThree is the name of the node defined in treeView1. But uses default in this example.
35                    {
36                        UserControl3 usrCtrl = new UserControl3();
37                        showUserControl(mySplitContainer, usrCtrl);
38                    }
39                    break;
40            }
41        }
42
43        /// <summary>
44        /// Shows the selected usercontrol in panel2 of a splitcontainer.
45        /// How to use:
46        /// UserControl1 usrCtrl1 = new UserControl1();
47        /// showUserControl(splitContainer1, usrCtrl1);
48        /// </summary>
49        /// <param name="mySplitContainer">Name of the SplitContainer.</param>
50        /// <param name="myUserControl">Name of the UserControl.</param>
51        private static void showUserControl(SplitContainer mySplitContainer, UserControl myUserControl)
52        {
53            mySplitContainer.Panel2.Controls.Add(myUserControl);
54            myUserControl.Dock = DockStyle.Fill;
55            myUserControl.Show();
56        }
57
58        /// <summary>
59        /// "Refreshes" panel2 by removing the UserControl.
60        /// How to use:
61        /// refreshPanel(splitContainer1);
62        /// </summary>
63        /// <param name="mySplitContainer">Name of SplitContainer.</param>
64        private static void refreshPanel(SplitContainer mySplitContainer)
65        {
66            if (mySplitContainer.Panel2.Controls.Count > 0)
67            {
68                mySplitContainer.Panel2.Controls.Remove(mySplitContainer.Panel2.Controls[0]);
69            }
70        }
Usage
* Create a form
* Add a SplitContainer to the form
* Add a TreeView control to panel1 of the SplitContainer
* Create one or more UserControls (Add->New Item...> User Control)
* Edit the TreeView control in designview and add one or more nodes. Give the nodes a name and a text.
* Edit the names in the switch statement to match the names of the nodes defined in the ThreeView control.
* Edit the UserControl-names in the switch statement to match the names of the created UserControls.
* Go to designview and doubleclick the TreeView to go to the handling of the "treeView1_AfterSelect".
* Add: findSelectedNode(splitContainer1, e); //splitContainer1 is the name of your added SplitContainer