Open Existing form from String

BrentP

New member
Joined
Nov 5, 2008
Messages
2
Programming Experience
5-10
I am passing a string value to my application with the name of an unopened form, and I need to use that string name to open up the corresponding form.


Is there any way to do that?
 
Last edited:

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Something like this.. using a bit of reflection.

VB.NET:
Imports System.Reflection

Dim frm As Form = DirectCast(Activator.CreateInstance(Type.GetType("yourApplication.someFormName") ), Form)
frm.Show()
 

BrentP

New member
Joined
Nov 5, 2008
Messages
2
Programming Experience
5-10
Much Appreciated

Thank You. I had scoured the Internet, but was unable to find any solution without a direct reference to the existing form.
 

Rod

Member
Joined
Oct 10, 2008
Messages
11
Location
Peterborough, UK
Programming Experience
1-3
hhmmm... This has got me thinking,

Would this work in a similar way with any object?

I have several Textboxes and ComboBoxes that are created/destroyed at runtime and named txt1, txt2 .... txtn. At the moment, whenever I need to use a control, I use:

VB.NET:
For Each ctrl in me.Controls
   If ctrl.Name = <String> Then
      'Whatever
   end if
next

The above could come in very very handy
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
Rod said:
whenever I need to use a control, I use:
me.Controls("the name") give you the control, or Me.Controls.Find("the name", True) to search all child containers. Reflection is useful to know, but there could be better alternatives.
 

Latest posts

Top Bottom