Click here to Skip to main content
Click here to Skip to main content

Call Form with string name ( convert String to Control)

By , 30 Oct 2012
 

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

About the Author

hajivalie
Engineer IceSoft.ir
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
http://www.hajivalie.com/About.html

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionlinkmemberHadi Basiri26 Nov '12 - 3:17 
ba salm.chera link download kar nemikone.
AnswerRe: linkmemberhajivalie26 Nov '12 - 18:06 
salam ehtemalan moshkel az codeproject hastesh
vali link dovom kar mikone
mer30
GeneralRe: linkmemberHadi Basiri7 Dec '12 - 19:30 
yani ..r........ sa.........af......it
GeneralMy vote of 5memberHadi Basiri4 Nov '12 - 20:13 
very good and perfect.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 30 Oct 2012
Article Copyright 2012 by hajivalie
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid