Call Form with string name ( convert String to Control)
Call Form with string name
Introduction
Whit This Code You Can Call Any Control Whit String.
in this sample you Convert String Name Of project And form like this(WindowsApplication1.Form2) to Control and call it.
if you want call control from another project must be Reflect Assembly Address of this. for example Assembly Address of my project is: "C:\StrToCtrl\WindowsApplication1\bin\Release\WindowsApplication1.exe" and also you can find Assembly address of current Project with this: System.Reflection.Assembly.GetExecutingAssembly().Location
if You Want Passing Data To your Form You can enter data in the second input this function: Activator.CreateInstance(FormInstanceType, New Object() {"hajivalie.com"}) in This example "hajivalie.com" is a string that passing to Form
Using the code
Call Form with string name
code:
Dim ProjAndForm = "WindowsApplication1.Form2" '' String Of Your Project And Form Name
Dim objType As Type = Type.[GetType](ProjAndForm) '' Get Type Of your string
Dim objForm As Control = DirectCast(Activator.CreateInstance(objType), Control) '' Convert Type to Control and now you can use this Control
objForm.Show()
Call Form with string name from another project
code:
Dim path As String = System.Reflection.Assembly.GetExecutingAssembly().Location '' Assembly Address Of Another Project!
Dim ass As Reflection.Assembly = Reflection.Assembly.LoadFrom(path)
Dim ProjAndForm = "WindowsApplication2.Form1"
Dim FormInstanceType = ass.GetType(ProjAndForm)
Dim objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
objForm.Show()
Call Form with string name with passing data
code:
Dim path As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim ass As Reflection.Assembly = Reflection.Assembly.LoadFrom(path)
Dim ProjAndForm = "WindowsApplication1.Form3"
Dim FormInstanceType = ass.GetType(ProjAndForm)
Dim objForm = CType(Activator.CreateInstance(FormInstanceType, New Object() {"hajivalie.com"}), Form)
objForm.Show()