Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Instantiate a form by name

How can I instantiate a form by its name?

somewhat like this....
Dim myForm as New ("frmMainMenu")
0
killer5
Asked:
killer5
1 Solution
 
RonaldBiemansCommented:
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
0
 
wguerramCommented:
You need to provide the Full path of the form.

CType(Activator.CreateInstance(Type.GetType("WindowsApplication6.Form1")), Form).Show()
0
 
iboutchkineCommented:

      Dim sForm As String = "WindowsApplication1.Form2"

        Dim frm As Form = _
              DirectCast(Activator.CreateInstance(Type.GetType(sForm)), Form)
        frm.Show()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wguerramCommented:
iboutchkine, what's the difference between your code and mine?
0
 
farsightCommented:
 [NOT AN ANSWER TO THE QUESTION]

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?
0
 
wguerramCommented:
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.

:-)
0
 
killer5Author Commented:
I really appreciate your comments guys!

thanks a lot!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now