Open a form based on a variable

thor80

New member
Joined
May 10, 2007
Messages
1
Programming Experience
3-5
I have a list box. The user chooses an item which passes the value of the item to a string.

Typically, I open forms by using this code:

My.Forms.FormName.Open()

Is there a way of opening the form using my string variable? I just can't seem to find a way.
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ShowForm([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] FormName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] ProjectName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = _[/SIZE]
[SIZE=2]  Reflection.Assembly.GetExecutingAssembly.GetName.Name[/SIZE]
 
[SIZE=2][COLOR=#0000ff]  Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] tyOfStringVariable [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Type = _[/SIZE]
[SIZE=2]     Type.GetType(ProjectName & [/SIZE][SIZE=2][COLOR=#a31515]"."[/COLOR][/SIZE][SIZE=2] & FormName)[/SIZE]
[SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] frmObject [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2] = Activator.CreateInstance(tyOfStringVariable)[/SIZE]
[SIZE=2][COLOR=#0000ff]     DirectCast[/COLOR][/SIZE][SIZE=2](frmObject, Form).ShowDialog()[/SIZE]
[SIZE=2][COLOR=#0000ff]  Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]Console.WriteLine(ex.Message)[/SIZE]
[SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Back
Top