Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Call Form with string name ( convert String to Control)

0.00/5 (No votes)
30 Oct 2012 1  
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()   

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here