Solved

Change string to form

Posted on 2009-05-18
5
330 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
[X]
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
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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