Displayin specific UserControl for selected node in vb.net

Hakan
Hakan used Ask the Experts™
on
I'm using TreeView as my navigation. When i'm selecting the node i'd like to load UserControl on next panel near navigation. I'm starting an example work but i realize it's not logic way for coding. I have about 15-20 nodes and it means i will copy same logic code for 15-20 times. I didn't find a solution to make it easier if there will be a dictionary for (node,usercontrol) it'd be easy but i can't create such a dictionary.

My example code is like below.
    Private Sub treToolNavigation_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles treToolNavigation.AfterSelect
        Dim node As TreeNode = treToolNavigation.SelectedNode
        Select Case node.Text
            Case "node1"
                Dim f As New UserControl1
                SplitContainer2.Panel2.Controls.Add(f)
                f.Dock = DockStyle.Fill
                f.Show()
            Case "node2"
                Dim f As New UserControl2
                SplitContainer2.Panel2.Controls.Add(f)
                f.Dock = DockStyle.Fill
                f.Show()
            Case "node3"
                Dim f As New UserControl2
                SplitContainer2.Panel2.Controls.Add(f)
                f.Dock = DockStyle.Fill
                f.Show()
        End Select
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You could always use the Tag property of the node; e.g. -

Form1.cs -
using System;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29142992
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var controls = new[] { typeof(ListBox), typeof(TextBox), typeof(ListView), typeof(ComboBox), typeof(DataGridView) };
            foreach (var i in Enumerable.Range(0, 15))
            {
                var node = new TreeNode($"UserControl{i}");
                node.Tag = controls[(i + (controls.Length - 1)) % controls.Length];
                treeView1.Nodes.Add(node);
            }
        }

        private void OnAfterSelect(object sender, TreeViewEventArgs e)
        {
            foreach (var control in splitContainer1.Panel2.Controls.Cast<Control>().Reverse())
            {
                if (!(control is Label))
                {
                    control.Dispose();
                    splitContainer1.Panel2.Controls.Remove(control);
                }
            }

            var instance = Activator.CreateInstance(e.Node.Tag as Type);
            label1.Text = instance.GetType().ToString();
            (instance as Control).Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(instance as Control);
            (instance as Control).Show();
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29142992
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(266, 450);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelect);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.label1);
            this.splitContainer1.Size = new System.Drawing.Size(800, 450);
            this.splitContainer1.SplitterDistance = 266;
            this.splitContainer1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(0, 422);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(530, 28);
            this.label1.TabIndex = 0;
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.splitContainer1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.Label label1;
    }
}

Open in new window

Capture.PNGCapture.PNGCapture.PNGCapture.PNGCapture.PNG-saige-

Author

Commented:
Hi saige, thanks for your reply. I guess it's really advanced way of doing that. But i have some static TreeViewNode and UserControls for each of node i only want to show UserControl for selected node.

In your way it requires so much modification on designer file.

I think my case is simple for you?

Commented:
You shouldn't really need to modify your designer file.  You simply need to attach the Type of the user control to the Tag property of your Nodes.  You could accomplish this in the OnLoad event or the constructor of the parent form.  Once you have the Tag properties for all of your nodes set then the AfterSelect event becomes semi generic.

Form1.cs -
using System;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29142992
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var controls = new[] { typeof(ListBox), typeof(TextBox), typeof(ListView), typeof(ComboBox), typeof(DataGridView) };
            foreach (var element in treeView1.Nodes.Cast<TreeNode>().Select((n, i) => new { Node = n, Index = i }))
            {
                element.Node.Text = $"UserControl{element.Index}";
                element.Node.Tag = controls[((element.Node.Index + controls.Length) - 1) % controls.Length];
            }
        }

        private void OnAfterSelect(object sender, TreeViewEventArgs e)
        {
            foreach (var control in splitContainer1.Panel2.Controls.Cast<Control>().Reverse())
            {
                if (!(control is Label))
                {
                    control.Dispose();
                    splitContainer1.Panel2.Controls.Remove(control);
                }
            }

            var instance = Activator.CreateInstance(e.Node.Tag as Type);
            label1.Text = instance.GetType().ToString();
            (instance as Control).Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(instance as Control);
            (instance as Control).Show();
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29142992
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Node0");
            System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node1");
            System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node2");
            System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node3");
            System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Node4");
            System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Node5");
            System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Node6");
            System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("Node7");
            System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("Node8");
            System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("Node9");
            System.Windows.Forms.TreeNode treeNode11 = new System.Windows.Forms.TreeNode("Node10");
            System.Windows.Forms.TreeNode treeNode12 = new System.Windows.Forms.TreeNode("Node11");
            System.Windows.Forms.TreeNode treeNode13 = new System.Windows.Forms.TreeNode("Node12");
            System.Windows.Forms.TreeNode treeNode14 = new System.Windows.Forms.TreeNode("Node13");
            System.Windows.Forms.TreeNode treeNode15 = new System.Windows.Forms.TreeNode("Node14");
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            treeNode1.Name = "Node0";
            treeNode1.Text = "Node0";
            treeNode2.Name = "Node1";
            treeNode2.Text = "Node1";
            treeNode3.Name = "Node2";
            treeNode3.Text = "Node2";
            treeNode4.Name = "Node3";
            treeNode4.Text = "Node3";
            treeNode5.Name = "Node4";
            treeNode5.Text = "Node4";
            treeNode6.Name = "Node5";
            treeNode6.Text = "Node5";
            treeNode7.Name = "Node6";
            treeNode7.Text = "Node6";
            treeNode8.Name = "Node7";
            treeNode8.Text = "Node7";
            treeNode9.Name = "Node8";
            treeNode9.Text = "Node8";
            treeNode10.Name = "Node9";
            treeNode10.Text = "Node9";
            treeNode11.Name = "Node10";
            treeNode11.Text = "Node10";
            treeNode12.Name = "Node11";
            treeNode12.Text = "Node11";
            treeNode13.Name = "Node12";
            treeNode13.Text = "Node12";
            treeNode14.Name = "Node13";
            treeNode14.Text = "Node13";
            treeNode15.Name = "Node14";
            treeNode15.Text = "Node14";
            this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode1,
            treeNode2,
            treeNode3,
            treeNode4,
            treeNode5,
            treeNode6,
            treeNode7,
            treeNode8,
            treeNode9,
            treeNode10,
            treeNode11,
            treeNode12,
            treeNode13,
            treeNode14,
            treeNode15});
            this.treeView1.Size = new System.Drawing.Size(266, 450);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelect);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.label1);
            this.splitContainer1.Size = new System.Drawing.Size(800, 450);
            this.splitContainer1.SplitterDistance = 266;
            this.splitContainer1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(0, 422);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(530, 28);
            this.label1.TabIndex = 0;
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.splitContainer1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.Label label1;
    }
}

Open in new window

Produces the same results as above.

-saige-
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
Hi saige, Now i understood more clear :) But i have only one type of user control i mean System.Windows.Forms.UserControl

Commented:
Your example made it seem as if you have multiple.  Are you needing specific instances for each node?

-saige-

Author

Commented:
Yes i'll use as navigation menu(static / not changed on runtime or etc.). When i'm selecting a node from TreeView, on the right panel the UserControl of that node will load, first i thought to load WinForm instead of UserControl but i thought and decide to use UserControl.

So i'm designing a user control form for each node and want to show them when node selected.

Commented:
You may have one base type; e.g. - UserControl, meaning a that all of your UserControls have the same base class, but you still will have different child types; e.g. -

Form1.cs -
using System;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29142992
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var controls = new[] { typeof(ListBoxUserControl), typeof(TextBoxUserControl), typeof(ListViewUserControl), typeof(ComboBoxUserControl), typeof(DataGridViewUserControl) };
            foreach (var element in treeView1.Nodes.Cast<TreeNode>().Select((n, i) => new { Node = n, Index = i }))
            {
                element.Node.Text = $"UserControl{element.Index}";
                element.Node.Tag = controls[((element.Node.Index + controls.Length) - 1) % controls.Length];
            }
        }

        private void OnAfterSelect(object sender, TreeViewEventArgs e)
        {
            foreach (var control in splitContainer1.Panel2.Controls.Cast<Control>().Reverse())
            {
                if (!(control is Label))
                {
                    control.Dispose();
                    splitContainer1.Panel2.Controls.Remove(control);
                }
            }

            var instance = Activator.CreateInstance(e.Node.Tag as Type);
            label1.Text = instance.GetType().ToString();
            (instance as Control).Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(instance as Control);
            (instance as Control).Show();
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29142992
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Node0");
            System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node1");
            System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node2");
            System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node3");
            System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Node4");
            System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Node5");
            System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Node6");
            System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("Node7");
            System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("Node8");
            System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("Node9");
            System.Windows.Forms.TreeNode treeNode11 = new System.Windows.Forms.TreeNode("Node10");
            System.Windows.Forms.TreeNode treeNode12 = new System.Windows.Forms.TreeNode("Node11");
            System.Windows.Forms.TreeNode treeNode13 = new System.Windows.Forms.TreeNode("Node12");
            System.Windows.Forms.TreeNode treeNode14 = new System.Windows.Forms.TreeNode("Node13");
            System.Windows.Forms.TreeNode treeNode15 = new System.Windows.Forms.TreeNode("Node14");
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            treeNode1.Name = "Node0";
            treeNode1.Text = "Node0";
            treeNode2.Name = "Node1";
            treeNode2.Text = "Node1";
            treeNode3.Name = "Node2";
            treeNode3.Text = "Node2";
            treeNode4.Name = "Node3";
            treeNode4.Text = "Node3";
            treeNode5.Name = "Node4";
            treeNode5.Text = "Node4";
            treeNode6.Name = "Node5";
            treeNode6.Text = "Node5";
            treeNode7.Name = "Node6";
            treeNode7.Text = "Node6";
            treeNode8.Name = "Node7";
            treeNode8.Text = "Node7";
            treeNode9.Name = "Node8";
            treeNode9.Text = "Node8";
            treeNode10.Name = "Node9";
            treeNode10.Text = "Node9";
            treeNode11.Name = "Node10";
            treeNode11.Text = "Node10";
            treeNode12.Name = "Node11";
            treeNode12.Text = "Node11";
            treeNode13.Name = "Node12";
            treeNode13.Text = "Node12";
            treeNode14.Name = "Node13";
            treeNode14.Text = "Node13";
            treeNode15.Name = "Node14";
            treeNode15.Text = "Node14";
            this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode1,
            treeNode2,
            treeNode3,
            treeNode4,
            treeNode5,
            treeNode6,
            treeNode7,
            treeNode8,
            treeNode9,
            treeNode10,
            treeNode11,
            treeNode12,
            treeNode13,
            treeNode14,
            treeNode15});
            this.treeView1.Size = new System.Drawing.Size(266, 450);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelect);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.label1);
            this.splitContainer1.Size = new System.Drawing.Size(800, 450);
            this.splitContainer1.SplitterDistance = 266;
            this.splitContainer1.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(0, 422);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(530, 28);
            this.label1.TabIndex = 0;
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.splitContainer1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.Label label1;
    }
}

Open in new window

Produces a similar output as above with the exception that the label shows the UserControl types.

-saige-

Commented:
If the above doesn't make any sense, perhaps this may.  Assuming you are using a naming standard to identify your UserControls and linking them to their node; e.g. node1 => node1UserControl; node2 => node2UserControl; etc...

Here is a quick implementation using 5 nodes and 5 usercontrols.

Capture.PNGForm1.cs -
using System;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29142992
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var controls = typeof(EE_Q29142992.Program).Assembly.GetTypes().Where(t => t.BaseType.Equals(typeof(UserControl))).ToArray();
            foreach (var element in treeView1.Nodes.Cast<TreeNode>())
            {
                element.Tag = controls.FirstOrDefault(t => t.Name.StartsWith(element.Text));
            }
        }

        private void OnAfterSelect(object sender, TreeViewEventArgs e)
        {
            foreach (var control in splitContainer1.Panel2.Controls.Cast<Control>().Reverse())
            {
                control.Dispose();
                splitContainer1.Panel2.Controls.Remove(control);
            }

            var instance = Activator.CreateInstance(e.Node.Tag as Type);
            (instance as Control).Dock = DockStyle.Fill;
            splitContainer1.Panel2.Controls.Add(instance as Control);
            (instance as Control).Show();
        }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29142992
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Node1");
            System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Node2");
            System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("Node3");
            System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("Node4");
            System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("Node5");
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView1.Location = new System.Drawing.Point(0, 0);
            this.treeView1.Name = "treeView1";
            treeNode6.Name = "Node1";
            treeNode6.Text = "Node1";
            treeNode7.Name = "Node2";
            treeNode7.Text = "Node2";
            treeNode8.Name = "Node3";
            treeNode8.Text = "Node3";
            treeNode9.Name = "Node4";
            treeNode9.Text = "Node4";
            treeNode10.Name = "Node5";
            treeNode10.Text = "Node5";
            this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode6,
            treeNode7,
            treeNode8,
            treeNode9,
            treeNode10});
            this.treeView1.Size = new System.Drawing.Size(266, 450);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterSelect);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            this.splitContainer1.Size = new System.Drawing.Size(800, 450);
            this.splitContainer1.SplitterDistance = 266;
            this.splitContainer1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.splitContainer1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.splitContainer1.Panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.SplitContainer splitContainer1;
    }
}

Open in new window

Produces the following output -Capture.PNGCapture.PNG-saige-

Author

Commented:
I think that's what i am looking for but i couldn't implement this to my Vb.net project

            Dim controls = GetType([b]GUI[/b]).Assembly.GetTypes.Where(t >= t.BaseType.Equals(GetType(UserControl))).ToArray()
            For Each element In GUI.[b]treToolNavigation[/b].Nodes.Cast(Of TreeNode)
                element.Tag = controls.FirstOrDefault(t >= t.Name.StartsWith(element.Text))
            Next

Open in new window


GUI is my main form and treToolNavigation is TreeView

It shows an error for t is not declared and i dont know how to declare it. If you know "how?" could you please show ?
Commented:
In VB.NET you have to use either a Function (or Sub) method for your lambdas; e.g. -
Dim controls = GetType(GUI).Assembly.GetTypes.Where(Function(t) t.BaseType.Equals(GetType(UserControl)))
For Each element in treToolNavigation.Nodes.Cast(Of TreeNode)
    element.Tag = controls.FirstOrDefault(Function(t) t.Name.StartsWith(element.Text))
Next

Open in new window

-saige-
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
make it easier if there will be a dictionary for (node,usercontrol) it'd be easy but i can't create such a dictionary.

Why not?

It could be as simple as:
Public Class Form1

    Private UserControls As New Dictionary(Of String, UserControl)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UserControls.Add("node1", New UserControl1)
        UserControls.Add("node2", New UserControl2)
        UserControls.Add("node3", New UserControl3)
        For Each uc As UserControl In UserControls.Values
            uc.Dock = DockStyle.Fill
        Next
    End Sub

    Private Sub treToolNavigation_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles treToolNavigation.AfterSelect
        Dim node As String = treToolNavigation.SelectedNode.Text
        If UserControls.ContainsKey(node) Then
            Dim uc As UserControl = UserControls(node)
            SplitContainer2.Panel2.Controls.Clear()
            SplitContainer2.Panel2.Controls.Add(uc)
            uc.Show()
        End If
    End Sub

End Class

Open in new window

Author

Commented:
Dear friends Mike and Saige, Thanks for your helps. I have similar issue for showing windows forms for selected TreeViewNode. But this time windows forms belongs to other projects in same solution.

Could you please help me to solve?

https://www.experts-exchange.com/questions/29146405/Displaying-specific-project's-form-in-solution-for-selected-treeview-node-in-vb-net.html

Regards,
Hakan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial