Netnoobie Posted January 27, 2004 Posted January 27, 2004 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 Quote
decrypt Posted January 27, 2004 Posted January 27, 2004 what like Dim frmString As New NameofFormtobeopened 'Declare this between subs frmString.show ' and this in the sub like button1_click Quote
Administrators PlausiblyDamp Posted January 27, 2004 Administrators Posted January 27, 2004 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() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Netnoobie Posted January 27, 2004 Author Posted January 27, 2004 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. Quote
NK2000 Posted January 27, 2004 Posted January 27, 2004 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.. Quote
Administrators PlausiblyDamp Posted January 27, 2004 Administrators Posted January 27, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NK2000 Posted January 27, 2004 Posted January 27, 2004 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.. Quote
Leaders dynamic_sysop Posted January 27, 2004 Leaders Posted January 27, 2004 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) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.