Displaying specific project's form in solution for selected treeview node in vb.net

Hakan
Hakan used Ask the Experts™
on
Hello,

I've similar topic before and find a solution with help of friends in following topic.

Displayin specific UserControl for selected node in vb.net
https://www.experts-exchange.com/questions/29142992/Displayin-specific-UserControl-for-selected-node-in-vb-net.html

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


And this is after node sleect event to show selected user control.

    Private Sub ToolNavigationAfterSelect(sender As Object, e As TreeViewEventArgs) Handles treToolNavigation.AfterSelect

        For Each ctrl As Control In IndexModuleTools.Panel2.Controls.Cast(Of Control).Reverse()
            ctrl.Dispose()
            IndexModuleTools.Panel2.Controls.Remove(ctrl)
        Next
        Dim dynType As Type = CType(e.Node.Tag, Type)
        Dim instance As Control = CType(Activator.CreateInstance(dynType), Control)
        instance.Dock = DockStyle.Fill
        IndexModuleTools.Panel2.Controls.Add(instance)
        instance.Show()
    End Sub

--- NOW --- i'd like to show Windows.Forms of another project which is included my solution but i don't know how it would be possible?

Any helps would be grateful.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Forms are also controls (they inherit, indirectly, from the Control class).  Understanding that, you should be able to add a reference to the project(s) which contains the form(s) and add the forms to your controls collection.  After you have done that you simply need to identify the instances of you controls which are forms and choose the appropriate method to show your form (Show or ShowDialog).  If you want your users to complete the tasks on a form before going back to the parent form, use ShowDialog; e.g. -

Using the code posted in EE PAQ 29142992  as the parent form, I am going to create a new project with a dynamically generated form:.  Making some modifications to the original code -

Form1.cs -
using EE_Q29146405.Models;
using EE_Q29146405.UI;
using System;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29142992
{
    public partial class Form1 : Form
    {
        Person person = new Person { DateOfBirth = new DateTime(1956, 6, 15), FirstName = "Charlie", LastName = "Daniels", Role = Person.JobRole.Manager };
        Options options = new Options { AllowOverride = true, DelayTimeout = 2, LockTimeout = 15 };

        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var controls = GetType().Assembly.GetTypes().Where(t => t.BaseType.Equals(typeof(UserControl)))
                .Concat(typeof(DynamicForm<>).Assembly.GetTypes().Where(t => t.BaseType.Equals(typeof(Form)) && t.IsGenericType)).ToList();
            foreach (var element in treeView1.Nodes.Cast<TreeNode>())
            {
                if (Equals(element.Name, "Options"))
                {
                    element.Tag = new InstanceActivator { Type = typeof(DynamicForm<Options>), Arguments = new object[] { "Current Options", options } };
                }
                else if (Equals(element.Name, "Person"))
                {
                    element.Tag = new InstanceActivator { Type = typeof(DynamicForm<Person>), Arguments = new object[] { "Current Person", person } };
                }
                else
                {
                    element.Tag = new InstanceActivator { Type = 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 = default(object);
            var activator = e.Node.Tag as InstanceActivator;
            if (Equals(activator.Type.BaseType, typeof(UserControl)))
            {
                instance = Activator.CreateInstance(activator.Type);
                (instance as Control).Dock = DockStyle.Fill;
                splitContainer1.Panel2.Controls.Add(instance as Control);
                (instance as Control).Show();
            }
            else if (Equals(activator.Type.BaseType, typeof(Form)) && activator.Type.IsGenericType && activator.Type.Name.StartsWith("DynamicForm`1"))
            {
                instance = Activator.CreateInstance(activator.Type, activator.Arguments);
                (instance as Form).ShowInTaskbar = false;
                (instance as Form).StartPosition = FormStartPosition.CenterParent;
                (instance as Form).ShowDialog();
            }

        }
    }

    class InstanceActivator
    {
        public Type Type { get; set; }
        public object[] Arguments { get; set; }
    }
}

Open in new window

Form1.Desinger -
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("Node1");
            System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node2");
            System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node3");
            System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node4");
            System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Node5");
            System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("DynamicForm");
            System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("DynamicForm");
            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.Margin = new System.Windows.Forms.Padding(4);
            this.treeView1.Name = "treeView1";
            treeNode1.Name = "Node1";
            treeNode1.Text = "Node1";
            treeNode2.Name = "Node2";
            treeNode2.Text = "Node2";
            treeNode3.Name = "Node3";
            treeNode3.Text = "Node3";
            treeNode4.Name = "Node4";
            treeNode4.Text = "Node4";
            treeNode5.Name = "Node5";
            treeNode5.Text = "Node5";
            treeNode6.Name = "Options";
            treeNode6.Text = "DynamicForm";
            treeNode7.Name = "Person";
            treeNode7.Text = "DynamicForm";
            this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode1,
            treeNode2,
            treeNode3,
            treeNode4,
            treeNode5,
            treeNode6,
            treeNode7});
            this.treeView1.Size = new System.Drawing.Size(354, 554);
            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.Margin = new System.Windows.Forms.Padding(4);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.treeView1);
            this.splitContainer1.Size = new System.Drawing.Size(1067, 554);
            this.splitContainer1.SplitterDistance = 354;
            this.splitContainer1.SplitterWidth = 5;
            this.splitContainer1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1067, 554);
            this.Controls.Add(this.splitContainer1);
            this.Margin = new System.Windows.Forms.Padding(4);
            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

Now produces the following output -

Selecting the first DynamicForm -Capture.PNGAnd now the second -Capture.PNGObviously you do not need to use a dynamic form, any form will suffice.  Just ensure that you use the correct constructor, if no constructor is required then this gets simplified somewhat.

-saige-

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