Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello all, question. I am returning a string that contains a form name. This form name is the same as a form that needs to be opened. I really don't want to hardcode the form names and whatnot for future changes.

 

like:

Dim frmString as String

Dim objFrm as new frmString

objFrm.Show

 

...something like that. Is there a way to do this?

 

Thanks.

Bryan

  • Administrators
Posted

Any reason why you want to do it this way? After all all the form names are compiled into your exe anyway so you will need a recompile to change / add a form.....

 

But here is one possible way

 

Dim s As String = "WindowsApplication1.Form3"
Dim f As Form

Dim fType As Type = Type.GetType(s)
f = System.Activator.CreateInstance(fType)
f.Show()

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
Any reason why you want to do it this way? After all all the form names are compiled into your exe anyway so you will need a recompile to change / add a form.....

 

That's actually a very good point.

 

Thanks.

Posted

not always true, with .net framework you can be very flexible, i know many tools which generate their ui just from a xml file..

 

also games like AoE/RoN do that..

  • Administrators
Posted

Loading a layout from an XML file is fairly easy - however this would not introduce a new form class into the executable which is what the question would seem to be refering to.

 

If there is a need to load forms dynamically there are ways to achieve this (reflection being a good one).

 

XML will allow you to alter the layout but would give no easy way to provide code / event handling as far as I'm aware.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

that was just an example

i know tools which compile at runtime a c# file and so you could have your new form, but i think i missunderstood the poster, maybe because i didnt catch the aim of form loading..

  • Leaders
Posted

here's a quick example i knocked up for you ...

       Dim frm As String = Application.ProductName & ".Form2" '/// your form's name ( as a string ) where it says Form2 ( keeping the . there
       Dim o As Object = Activator.CreateInstanceFrom(Application.ExecutablePath, frm).Unwrap

       o.GetType.InvokeMember("Show", Reflection.BindingFlags.InvokeMethod, Nothing, o, Nothing)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...