Load Form at Run Time using vb.net

danishce

Member
Joined
Apr 2, 2007
Messages
15
Programming Experience
3-5
Hello,
I want to load a form dynamically by reading Form Name from a text file
using vb.net. In visual basic 6 code works fine but when i use the same
code in vb.net it gives the error.
The code below works perfect in visual basic 6:
Dim form_name As String
dim search_name() as string
form_name = search_name(0)
Dim obj As Form
Set obj = Forms.Add(form_name)
obj.Show
Basically it reads the name of the form from a text file and store it
in form_name variable, then it add that form in the form collection
using forms.add(form_name) and load that form succesfully.
but when i do that same code in vb.net, it gives the following error in
the line:
1)obj=forms.add(form_name)
(error: Name 'Forms' is not declared).
I try these things also,but it does not solve my problem.
2)obj = System.Windows.Forms.Add(form_name)
(error: 'Add' is not a member of 'Forms')
3)Dim obj As Form = DirectCast(form_name, Form)
obj.Show()
(error: Specified cast is not valid)
can anybody explain me how to achieve this in vb.net 2003?
Regards:
Danish Majid
 
With Reflection:
VB.NET:
Dim a As Reflection.Assembly = Me.GetType.Assembly
Dim f As Form = a.CreateInstance(a.GetName.Name & ".Form2")
f.Show()
Or simple cases:
VB.NET:
        Dim formname As String = "Form1"
        Dim frm As Form = Nothing
        Select Case formname
            Case "Form1"
                frm = New Form1
            Case "Form2"
                frm = New Form2
        End Select
        frm.Show()
 
The Problem is that i dont want to use If-Else /Select Case, However i get the form name from a text file and based on that name i load that existing form. for ex: i have added a form named "Main Menu". Now i want to read data from a text file and when form name matches it load "Main Menu" form. How can i do this:
Regards:
Danish Majid
 
Back
Top