I have two projects: SQLtesting and Controls. Their physical locations are:

C:\My Documents\Visual Studio 2010\Projects\SQLtesting\SQLtesting\ [forms reside here]
C:\My Documents\Visual Studio 2010\Projects\Controls\Controls\ [forms reside here]

The code shown below works fine. I run it from the project SQLtesting. It loads a listbox and a checkedlistbox with the controls found on a form. I can change the value of the FormName field to any form within the SQLtesting project and get the form's controls. The forms are not actually opened/shown and they are not suppose to be.

I would like to be able to access forms in other projects i.e.
C:\My Documents\Visual Studio 2010\Projects\Controls\Controls\
while running the code from the SQLtesting project.

In actual use, (when this application is an .exe) the user (developer) will enter the Project Name Form name, and whatever else this requires and click on Button7. There cannot be any hardcoding as in my example code. This application will be installed on individual pc's or on a network so the code must work based on what the developer enters.

The problem is getting the code shown to use what the user entered. I think it may
involve the 3 statements I have.

FullTypeName = Application.ProductName & "." & FormName
FormInstanceType = Type.GetType(FullTypeName, True, True)
objForm = CType(Activator.CreateInstance(FormInstanceType), Form)

Currently, if I change the FullTypeName to "Controls" and the FormName to "Form66" an exception is thrown on the FormInstanceType statement.

Could not load type 'Controls.Form66' from assembly 'SQLtesting,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

As I said above, the code works fine and provides exactly what I want as long as I
reference a form in the current project SQLtesting.

If somehow those statements can be modified or other statements added to use what
the user entered the problem would be solved.

Can anyone help me with this?

Thank you.

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

        Dim [B]FormName[/B] As String = "Form1"
        Dim [B]FullTypeName [/B]As String
        Dim [B]FormInstanceType[/B] As Type
        Dim objForm As Form

        [B]FullTypeName = Application.ProductName & "." & FormName
        FormInstanceType = Type.GetType(FullTypeName, True, True)
        objForm = CType(Activator.CreateInstance(FormInstanceType), Form)[/B]

        ListBox1.Sorted = True
        ListBox1.Items.Clear()
        CheckedListBox1.Sorted = True
        CheckedListBox1.Items.Clear()
        CheckedListBox1.CheckOnClick = True

        Dim CtlType As Type
        Dim TypeName1 As String

        For Each ctl As Control In objForm.Controls  
            CtlType = ctl.GetType
            TypeName1 = CtlType.ToString.Substring(21) ' Remove system.windows.forms prefix
            Dim index As Integer = CheckedListBox1.FindStringExact(TypeName1)
            If index = -1 Then
                CheckedListBox1.Items.Add(TypeName1)
            End If
            TypeName1 = TypeName1 & ":" & ctl.Name & ":" & ctl.Text
            ListBox1.Items.Add(TypeName1.ToString)
        Next ctl

    End Sub

I think that line FullTypeName = Application.ProductName & "." & FormName goes wrong because you use Application.ProductName which is "SQLtesting".

Instead of that you should reference to assembly:

Imports System.Reflection

Dim assemblyName As String = "<here's the name of your other assembly>"
Dim oAssembly As Assembly = Nothing ' the assembly gets loaded to this
Dim oInstance As Object ' and here's an instance of the assembly

' Load assembly
oAssembly = Assembly.LoadFile(assemblyName)
' Create an instance of the assembly
oInstance = oAssembly.CreateInstance("<typeName goes here>", True)
' or as a typed instance
oTypedInstance = CType(oInstance, <typeName goes here>)
' Now you refer form
Dim otherForm as Form = oTypedInstance.Form66

Sort of pseudo-code but the point is to use Assembly instead of the Application object.

HTH

Thank you Teme64 for your respone. I can't get it to work and I'm lost. I don't know what to put in assemblyName or typeName field you reference in your code
oInstance = oAssembly.CreateInstance("<typeName goes here>", True). There isn't a DIM for oTypedInstance.

I can't use the statement:
dim otherForm as Form = oTypedInstance.Form66because nothing can be hardcoded "Form66".

What information can I supply you with so you can tell me what to code?

Thank you for your time.

Add a treeview to the form.

You closed the other thread out but I don't know why. This should do what your looking for. If it does not, please state what it doesn't do.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Change the assemblyFile location to 
        'C:\My Documents\Visual Studio 2010\Projects\Controls\Controls\bin\debug\Controls.exe
        'Change the type name to Form1 or whatever name your looking for
        'It should be shown in the treeview.

        LoadTypesFromAssemblyFile("C:\Users\Administrator\Documents\Visual Studio 2008\Projects\CustomCombobox\CustomCombobox\bin\Debug\CustomCombobox.exe", "frmForum")

    End Sub

    Private Sub LoadTypesFromAssemblyFile(ByVal assemblyFileLocation As String, Optional ByVal TypeName As String = "")

        Dim AssemblyFromFile As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(assemblyFileLocation)
        Dim Types As Type() = AssemblyFromFile.GetTypes
        Dim TreeNodeControl As TreeNode
        Dim ControlNode As TreeNode

        For Each Type As Type In Types

            'If a type name was passed in then check for the name
            'otherwise load all types into the treeview.
            If Not String.IsNullOrEmpty(TypeName) Then
                If Type.Name = TypeName Then
                    Dim Control As Control = CType(Activator.CreateInstance(Type), Control)

                    TreeNodeControl = New TreeNode(Type.Name)
                    TreeNodeControl.Nodes.Add("Name: " & Control.Name)
                    TreeNodeControl.Nodes.Add("Text: " & Control.Text)

                    ControlNode = TreeNodeControl.Nodes.Add("Controls")
                    For Each ChildControl As Control In Control.Controls
                        ControlNode.Nodes.Add(ChildControl.GetType.Name)
                    Next

                    TreeView1.Nodes.Add(TreeNodeControl)

                End If
            Else
                'A type name was not passed in.  Such as Form1
                'Load all types into the treeview that derive from
                'control.
                If Type.IsSubclassOf(GetType(Control)) Then

                    Dim Control As Control = CType(Activator.CreateInstance(Type), Control)

                    TreeNodeControl = New TreeNode(Type.Name)
                    TreeNodeControl.Nodes.Add("Name: " & Control.Name)
                    TreeNodeControl.Nodes.Add("Text: " & Control.Text)

                    ControlNode = TreeNodeControl.Nodes.Add("Controls")
                    For Each ChildControl As Control In Control.Controls
                        ControlNode.Nodes.Add(ChildControl.GetType.Name)
                    Next

                    TreeView1.Nodes.Add(TreeNodeControl)


                End If
            End If
          

        Next
    End Sub

Edited 5 Years Ago by Unhnd_Exception: n/a

Unhnd_Exception:
I truly appreciate the time and effort you put in responding to me. I created a TreeView and copied your code and ran it. It worked just as you intended. It isn’t exactly what I’m looking for. What I really need is some statements that POINT to another project but still use my code because it does exactly what I want. These statements would be prior to the following 3 statements shown in my code.

FullTypeName = Application.ProductName & "." & FormName
FormInstanceType = Type.GetType(FullTypeName, True, True)
objForm = CType(Activator.CreateInstance(FormInstanceType), Form)


The above statements may have to be modified and that’s fine. I just need it to work with my existing code that follows.

Good Luck!

BTW

Your three lines of code DO NOT WORK.

I guess I'm confused of what you want. I thought you wanted to load an external assembly, a windows forms app, and find an object by the name and then list its type name, instance name, and instance text. Let me know what you figure out.

The Solution to your problem is staring you in the face. You just have not put the pieces together.

Hasta La Pasta,
Unhnd

This article has been dead for over six months. Start a new discussion instead.