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=188.8.131.52, 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?
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