Opening forms using strings

wdw

Freshman
Joined
Dec 11, 2002
Messages
37
I've this code:
Dim f As Form, s As String
s = "Form1"
f = Forms.add(s)
f.Show()

I've the following problem: vb.net does not recognize Forms.Add

Can someone give me the correct code for this?

thanks
 
Hope this helps

we wnated form 1 to hide and then form 2 to show so this is the code we used:

I don't know if it's exactly what you're looking for

Me.Hide()
Dim F As New Form2()
'we inserted variables that will pass to form2 here.
F.Show()

sorry if this isn't what you need.

thanks! Mindi
 
Ok, I'll explain what I am trying to do:
I've have a menu where a user can select a menuitem.
if he selects an item, he must go to a specific page.
The pagename where he must go to comes from the database.
But I cannot get the right code.

Hope you've got a good way to do this.

greetz
 
The name of the form (class) is stored as a string in the database? So if you have a class named frmMain and you have a string with the value "frmMain" you want to instantiate frmMain (the class) from the string value?

-ner
 
That really does seem an awfully odd way of doing things. However, if you really need it it should be possibly through the Assembly class:

Visual Basic:
Dim f As Form

f = DirectCast(System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("WindowsApplication1.myForm"), Form)
f.Show()

Where "WindowsApplication1.myForm" is the full-qualified (i.e. including namespace) name of your form in your project.
 
i've tried your code, but this is my result:

An unhandled exception of type 'System.NullReferenceException' occurred in application.exe

Additional information: Object reference not set to an instance of an object.

Additional information from me: The form does exist, I use the correct namespace, I have the destination form in a folder in my solution.

I don't get it anymore!!:confused:
 
I'm sorry, but that is the correct and only way of doing what you need. If you're getting that exception, it means you got the namespace wrong.

Right-click on your project and select properties. In the box that says Root Namespace will be the namespace you want. Providing you haven't specified a sub-namespace in your form class (I'm guessing you haven't) then you can just use this with your form name in the code I gave you.

If your root namespace is WindowsApplication1, the string you'd pass to CreateInstance is WindowsApplication1.Form1, or whatever the name of your form is.
 
Thank you very much, I didn't ad the root namespace to my string, so that was the solution to make it work alright.

greetz willem
 
Back
Top