Reputation: 3
I am using VB.net 2010 and 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 ProjAndForm 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.
What I really need is a way to POINT to another project and form and still use my code. I believe the statements needed would precede the following 3 statements I have.
ProjAndForm = "SQLtesting.Form1"
FormInstanceType = Type.GetType(ProjAndForm, True, True)
objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
Currently, if I change the value of ProjAndForm to "Controls.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.
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 CtlType As Type
Dim FormInstanceType As Type
Dim index As Integer
Dim objForm As Form
Dim ProjAndForm As String
Dim TypeName1 As String
Dim TypeName2 As String
ListBox1.Sorted = True
ListBox1.Items.Clear()
CheckedListBox1.Sorted = True
CheckedListBox1.Items.Clear()
CheckedListBox1.CheckOnClick = True
ProjAndForm = "SQLtesting.Form1"
FormInstanceType = Type.GetType(ProjAndForm, True, True)
objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
For Each ctl As Control In objForm.Controls
CtlType = ctl.GetType
TypeName1 = CtlType.ToString.Substring(21) ' Remove system.windows.forms prefix
index = 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
Upvotes: 0
Views: 4851
Reputation: 5029
What you are looking for is Assembly.LoadFrom. You will need the exe or dll for this.
Dim path As String = "youassemblywitpath.dll"
Dim ass As Assembly = Assembly.LoadFrom(path)
ProjAndForm = "SQLtesting.Form1"
FormInstanceType = ass.GetType(ProjAndForm)
objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
or you could use MEF.
Upvotes: 1