Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Call Form with string name ( convert String to Control)

Rate me:
Please Sign up or sign in to vote.
3.67/5 (4 votes)
30 Oct 2012CPOL 30.2K   567   2   9
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: 

VB.NET
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:

VB.NET
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:

VB.NET
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncontrols on form Pin
CDKSO1-Mar-23 3:50
CDKSO1-Mar-23 3:50 
Questionis there a way passing data to form without creating an instance and pass data Pin
asero2317-Sep-15 15:49
asero2317-Sep-15 15:49 
QuestionCall From Another Project Not Working Pin
khaledtamam6-Oct-14 22:01
khaledtamam6-Oct-14 22:01 
AnswerRe: Call From Another Project Not Working Pin
hajivalie12-Dec-14 3:43
hajivalie12-Dec-14 3:43 
QuestionConvert String to ToolStripMenuItem Pin
Member 1086838829-Jul-14 7:58
Member 1086838829-Jul-14 7:58 
Questionlink Pin
Hadi Basiri26-Nov-12 3:17
Hadi Basiri26-Nov-12 3:17 
AnswerRe: link Pin
hajivalie26-Nov-12 18:06
hajivalie26-Nov-12 18:06 
GeneralRe: link Pin
Hadi Basiri7-Dec-12 19:30
Hadi Basiri7-Dec-12 19:30 
GeneralMy vote of 5 Pin
Hadi Basiri4-Nov-12 20:13
Hadi Basiri4-Nov-12 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.