Opening forms using strings

wdw

Active member
Joined
Dec 11, 2002
Messages
37
Ive this code:
Dim f As Form, s As String
s = "Form1"
f = Forms.add(s)
f.Show()

Ive 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 dont know if its exactly what youre looking for

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

sorry if this isnt what you need.

thanks! Mindi
 
There isnt a Forms collection in VB.NET, and I havent a clue what youre trying to do.
 
Ok, Ill explain what I am trying to do:
Ive 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 youve 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:

Code:
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.
 
ive 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 dont get it anymore!!:confused:
 
Im sorry, but that is the correct and only way of doing what you need. If youre 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 havent specified a sub-namespace in your form class (Im guessing you havent) then you can just use this with your form name in the code I gave you.

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

greetz willem
 
Back
Top