Solved

Change string to form

Posted on 2009-05-18
5
319 Views
Last Modified: 2013-11-25
Hi there, i want to change a string to a form name, for example:

dim FormName as string = Form2

dim newform as form = FormName

newform.show()


From what I've read I need to use system.reflection for this but I'm stabbing wildly in the dark.

Thanks!
0
Comment
Question by:garethh86
5 Comments
 
LVL 1

Expert Comment

by:Modar_Hijazi
ID: 24411323
Try the following:
        Dim FormName As String = "My Form"
        Dim newform As Form = New Form
        newform.Name = FormName
        newform.Show()

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24411364
You should be able to create a new form and give the form a name.

Dim FormName as String = Form2
Dim newform as new Form()
newForm.Name = FormName

newForm.Show
0
 
LVL 7

Author Comment

by:garethh86
ID: 24411444
Sorry, that just creates a brand new form, I already have the form created. Im trying to open this form and add to a panel from a treeview based on its tag name.
0
 
LVL 1

Assisted Solution

by:Modar_Hijazi
Modar_Hijazi earned 100 total points
ID: 24411533
Your code must be changed to:

        Dim FormName As String = "Form2"
        Dim newform As Form = New Form
        newform = GetFormByName(FormName)
        newform.Show()

and the method GetFormByName is attached ;)
    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

Open in new window

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 400 total points
ID: 24411536
You are correct - reflection is required. The below example shows how.

Wayne
        Dim FormName As String = "Form2"
        Dim newform As Form = CType(Reflection.[Assembly].GetEntryAssembly.CreateInstance( _
                                    My.Application.Info.AssemblyName & "." & FormName), Form) 
        newform.Show()

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

831 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