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

Tagged as

Call Form with string name ( convert String to Control)

, 30 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hajivalie
Engineer IceSoft.ir
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
http://www.hajivalie.com/About.html
Follow on   Google+

Comments and Discussions

 
QuestionConvert String to ToolStripMenuItem PinmemberMember 1086838829-Jul-14 7:58 
Questionlink PinmemberHadi Basiri26-Nov-12 3:17 
AnswerRe: link Pinmemberhajivalie26-Nov-12 18:06 
GeneralRe: link PinmemberHadi Basiri7-Dec-12 19:30 
GeneralMy vote of 5 PinmemberHadi Basiri4-Nov-12 20:13 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 30 Oct 2012
Article Copyright 2012 by hajivalie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid