Switchboard in VB.net

  • Thread starter Thread starter Tom T via DotNetMonster.com
  • Start date Start date
T

Tom T via DotNetMonster.com

Hello all.. I'm making a dynamic menu in VB net for an aplication but i'm
having an issue ...

I need to use a varable as part of an argument when opening new forms..

example..

Dim X as string
x= rs.fields("formname")

dim View me as new X

how do I get VB.Net to reconize that x is an aculy form and create a new
instance of it...

Many thanks,

Tom T
 
Tom, If I understand you, you want to pass an argument to a new form when you
create the form. So you want code like:

Dim frm As New Form2(myVariable)

Then in the From2.vb code you need to add a constructor for the variable
provided. Like the following which changes the title of the new form to the
string that is passed to it on the creation. (ByVal is the default.)

Public Sub New(ByVal x)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
Me.Text = x
End Sub
 
My VB.NET is rusty, so hopefully you can figure out the equivalent code.
This c# code will work:

string x = rs.fields("formname")
// note that formname must be the full name of the class
// including the namespace
Type formType = Type.GetType(x);
System.Reflection.ConstructorInfo constructor =
formType.GetConstructor(new Type[0]);

Form newForm = (Form) constructor.Invoke(new object[0]);
newForm.Show();

The key methods to look at are the static Type.GetType() and instance
method Type.GetConstructor(). They will let you create an instance of
an class, when you only have the name of the class as a string.


Joshua Flanagan
http://flimflan.com/blog
 
Back
Top