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 }