Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load Form by Name (string) 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,502
US
VB.NET 2013, Win 7

I would like to know how to load a Form when I have the Name of the Form as String.

Eventually I may have 200+ forms in my application (I know, it is crazy, but they all are different) and I will have the names of the Forms in a table.

I can do a long Select Case, but calling Form by name would be so much shorter.

I did try this: Calling a Form with a string in VB.NET and this: Calling a separate Windows Form using its name as a String but the returned object was Nothing :-(

I did try this: Call Form with string name but... 'no cigar' either :-(

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
I've not looked at the links, but I would guess that they use the Reflection classes and as such should do the trick. I probably won't have much time to look into this until the latter half of the week. Post back on Thursday if you still do not have a working solution and I'll do some experimentation.
 
Yes, those examples do use [tt]System.Reflection[/tt], but like I said before, I ended up with :

[tt]obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

obj = Nothing [/tt]
:-(

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
The following illustrates one possible solution:

Code:
[blue]Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ShowFormbyName("Form2")
    End Sub

    Private Sub ShowFormbyName(strForm As String)
        System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & strForm).show()
    End Sub
End Class[/blue]
 
Works great, thank you.

I took the liberty to modify it a little bit so I have more control over the new Form:

Code:
Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click[blue]
    With FormByName("frmAndy")[/blue]
        .Location = New Point(Me.Location.X, Me.Location.Y)
        .Show()
    End With
End Sub

Private [blue]Function[/blue] FormByName(strFormName As String)[blue] As Form
    Return[/blue] System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & strFormName)
End Function

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top