Solved

Change string to form

Posted on 2009-05-18
5
316 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
Comment Utility
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 25

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now