Refer to Form by String (Visual Studio 2008)
Posted on 2009-12-20
How do I refer to a form in my project by using a string variable? I'm running Visual Studio 2008.
My main screen has a number of dropdown menus that, when an item is selected, a form will open:
Dim Mdi_frm_Doors As New frm_Doors
Mdi_frm_Doors.MdiParent = Me
In this example, I am providing the name of the form directly (frm_Doors). What I would like to do is refer to the form as a string variable.
I'm already doing something similar to access specific controls on other open forms (where str_Field is the name of the control I am accessing):
frm_1.Controls(str_Field).ForeColor = Color.Red
There's got to be something similar for forms. NOTE: The form I am trying to open is not already open, so I cannot use a loop like:
For Each CurrentForm As Form In Application.OpenForms
If CurrentForm.Name.IndexOf("frm_Cabinet_Assembly_Detail") >= 0 Then
I would prefer not to create an array of all forms in my project, but rather provide the name of a form as a string directly in the code. I've tried the following code, but I get a System.ArgumentException error for parameter typeName@0:
Dim str_Form As String = "frm_Construction_Methods"
Dim strFormToOpen As String = "[Solid Explorer]." & str_Form
Dim obhNew As Runtime.Remoting.ObjectHandle
obhNew = Activator.CreateInstance(Nothing, strFormToOpen) ' <<<< Error generated here.
Dim frmNew As Form = DirectCast(obhNew.Unwrap, Form)
I apologize if I have missed something obvious that has already been posted somewhere - I may not be searching for the correct terminology.
Thank you in advance for your help!