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.

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.

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 FullTypeName As String
        Dim FormInstanceType As Type
        Dim objForm As Form

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

        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

Recommended Answers

All 4 Replies

Member Avatar for Unhnd_Exception

You need to add a reference to the other project.

When you have SQL Testing open go to Project/Add Reference on the menu bar of visual studio. On the Add Reference Window you should see .net/project/... Select browse and navigate to the Controls Folder. Inside the Controls folder you should see a folder name bin and inside bin you should see debug or release. If you built the Controls project as debug then go into the debug folder and then open the Controls.exe file.

In Sqltesting you should then be able to type Controls.Whatever you have in the project.

Member Avatar for Unhnd_Exception

I was looking over your code. I didn't run it but see if this does the same thing. Other than the checklistbox.

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

        'After adding a reference to your Controls app
        'you should be able to do
        'CheckForm as new Controls.Form1
        Dim CheckForm As New Form2

        For Each C As Control In CheckForm.Controls
            'C.GetType.Name only has the name not
            'the assembly or namespace
            ListBox1.Items.Add(C.GetType.Name)

            ListBox2.Items.Add(C.GetType.Name & ":" & C.Name & ":" & C.Text)
        Next
    End Sub
Member Avatar for Unhnd_Exception

Maybe you are looking for something like this.

This will create a form from another project without adding it
as a reference.

'Get all the controls from all forms in another windows forms app
Dim AssemblyFromFile As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile("C:\Users\Administrator\Documents\Visual Studio 2008\Projects\CustomCombobox\CustomCombobox\bin\Debug\CustomCombobox.exe")
Dim Types As Type() = AssemblyFromFile.GetTypes

For Each Type As Type In Types

   If Type.BaseType Is GetType(Form) Then
        Dim F As Form = CType(Activator.CreateInstance(Type), Form)
        For Each C As Control In F.Controls
             MsgBox(C.Name)
        Next
   End If
Next

Thank you Unhnd_Exception for your effort and time providing 3 solutions. I feel you
are really close but not exactly what I need. I'm going to close this thread and
open a new one with more information. I hope you will review it and offer other
solutions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.