Solved

Change string to form

Posted on 2009-05-18
5
325 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Filesystem watcher not working 5 57
CSS question 16 63
C# rename a class and update all references efficiently 6 54
Manage big list of parameter list 8 25
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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…

738 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