Switchboard in VB.net

  • Thread starter Tom T via DotNetMonster.com
  • 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
 
G

Guest

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
 
J

Joshua Flanagan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top