Instantiate a form by name

Posted on 2005-03-03
Medium Priority
Last Modified: 2012-06-21
How can I instantiate a form by its name?

somewhat like this....
Dim myForm as New ("frmMainMenu")
Question by:killer5
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Accepted Solution

RonaldBiemans earned 1000 total points
ID: 13448143
You can do this by using reflection:

Public Sub ShowForm(frm_name as string)

        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

        Dim objForm As Object = asm.CreateInstance(asm.GetName.Name + "." + frm_name)

        Ctype(objForm, Form).show
End Sub

Expert Comment

ID: 13449026
You need to provide the Full path of the form.

CType(Activator.CreateInstance(Type.GetType("WindowsApplication6.Form1")), Form).Show()
LVL 28

Expert Comment

ID: 13450094

      Dim sForm As String = "WindowsApplication1.Form2"

        Dim frm As Form = _
              DirectCast(Activator.CreateInstance(Type.GetType(sForm)), Form)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13450935
iboutchkine, what's the difference between your code and mine?
LVL 12

Expert Comment

ID: 13451678

wguerram: Let me jump in here and take a shot at that.
[ I hope this serves as a good illustration of good coding practices, and how to write for the reader.  (No offense intended.  I'm not implying you don't do that.)  This is such a tiny piece of code, yet offers many possibilities of variation. I'm just trying to "plus" this discussion. ]

(1) sForm is broken out separately, which allows the rest of the code to be easily parameterized.  It better illustrates that the code can be used for any particular form name.
(2) It creates frm, a Form object, which can be used in later code to manipulate the form that's been created.
(3) It uses DirectCast.  This is better than CType in this case, because the programmer already knows that the thing that's being cast is a kind of Form (i.e. derived from the Form class).  Therefore DirectCast will always work, and it will be faster than CType.
(4) The code is broken up into 4 lines (3 statements), which makes the intent of each line clearer, and is easier to read.  It can also be easier to debug, because the exception/debugger will identify the line where the exception occurs, and this separates out the two critical parts of the function (creating the form, and showing the form).
(5) It answers the original question better.  It clearly breaks out the actual answer from the supporting code.  The actual answer is just the middle statement, without referring to a specific form name and without showing the form.
(6) It takes into account that the primary reader of the code is another programmer, not just the computer/compiler.
(7) It's readable even for beginning programmers, though the middle statement is still pretty difficult.  There's not too much happening all in the same line.
(8) Because it's well broken out, the second and third statements are copy-and-pastable.  It doesn't have that nasty string literal in the middle of it.

iboutchkine:  Anything to add?

Expert Comment

ID: 13452512
Well farsight, what can i say.

I am totally agree with you. it's just that at least iboutchkine would have mention that the code is kind of a derivation of mine, and explaining the reason why shouldn't use mine, as you did.

I was just thinking on making the code smaller.


Author Comment

ID: 13455886
I really appreciate your comments guys!

thanks a lot!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question