×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

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!

*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

Visual Basic (Classic) FAQ

Tip of the Day

Get Form by Name by SBendBuckeye
Posted: 19 Aug 05 (Edited 24 Aug 05)

This is a form equivalent to Get control by name using reflection FAQ796-5698 written by Chrissie1 a while ago.

I wanted to be able to create a form instance from the form name itself. Thanks to some threads on this forum, a bit of Googling and some MSDN work, I was able to come up with the following code. Enjoy!  

CODE

Imports System.Reflection
Imports System.windows.forms

'Creates form from form name - CASE SENSITIVE
Public Shared Function GetFormByName( _
    ByVal formName As String) As Form

    Dim assemblyName As String = _
        [Assembly].GetEntryAssembly().GetName.Name.Replace(" ", "_")
  
    Dim myType As Type = _
        Type.GetType(assemblyName & "." & formName)
  
    Return CType(Activator.CreateInstance(myType), Form)

End Function

Another way was sent to me by chrissie1

CODE

Private Function GetFormByName(ByVal formName As String) As Object

    Dim myasm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

    Try
        Return myasm.CreateInstance(myasm.GetName.Name.Replace(" ", "_") & "." & formName)

    Catch ex As Exception
        Return Nothing
    End Try

End Function

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

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