×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Load Form by Name (string)

Load Form by Name (string)

Load Form by Name (string)

(OP)
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 sad

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

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.

RE: Load Form by Name (string)

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.

RE: Load Form by Name (string)

(OP)
Yes, those examples do use System.Reflection, but like I said before, I ended up with :

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

obj = Nothing

sad

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.

RE: Load Form by Name (string)

The following illustrates one possible solution:

CODE

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 

RE: Load Form by Name (string)

(OP)
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
    With FormByName("frmAndy")
        .Location = New Point(Me.Location.X, Me.Location.Y)
        .Show()
    End With
End Sub

Private Function FormByName(strFormName As String) As Form
    Return 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.

RE: Load Form by Name (string)

Take all the liberties you like! smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close