Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to dynamic load or show form by Form name ?

How to dynamic load or show form by Form name ?

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Old Gun
    wrote on last edited by
    #1

    My project form are added dynamic,How are to load these forms by form name , like this. Form f = new Form(“FormName”); Thank you

    M B 2 Replies Last reply
    0
    • O Old Gun

      My project form are added dynamic,How are to load these forms by form name , like this. Form f = new Form(“FormName”); Thank you

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi Richard. This almost looks like MS Access Basic syntax :). In C#, forms are objects just like everything else, and are instantiated with their class name. If I have a form called "DocForm" in my project, I could add an instance of this form at runtime with the following:

      DocForm f = new DocForm();
      f.Show()

      Was this what you meant?

      1 Reply Last reply
      0
      • O Old Gun

        My project form are added dynamic,How are to load these forms by form name , like this. Form f = new Form(“FormName”); Thank you

        B Offline
        B Offline
        Baris Kurtlutepe
        wrote on last edited by
        #3

        You can use reflection. A primitive implementation would be like this: Type t = Type.GetType("FormName"); Form f = (Form) Activator.CreateInstance(t); That of course depends on what you mean with "forms are added dynamically", if the specified type is in the same assembly where the above code is executing, it should work.

        O 1 Reply Last reply
        0
        • B Baris Kurtlutepe

          You can use reflection. A primitive implementation would be like this: Type t = Type.GetType("FormName"); Form f = (Form) Activator.CreateInstance(t); That of course depends on what you mean with "forms are added dynamically", if the specified type is in the same assembly where the above code is executing, it should work.

          O Offline
          O Offline
          Old Gun
          wrote on last edited by
          #4

          Thank both of you! The second answer is What I wanted,I have test it works. Following is my test code ///--------------------------------------------------------------------------- private void button1_Click(object sender, System.EventArgs e) { Module[] moduleArray; moduleArray = Assembly.GetExecutingAssembly().GetModules(false); //In a simple project with only one module, the module at index // 0 will be the module containing these classes. Module myModule = moduleArray[0]; Type myType; myType = myModule.GetType("WindowsApplication1.Form2"); Form f = (Form) Activator.CreateInstance(myType); f.Show(); } ///------------------------------------------------------------------------- ///-------------------------------------------------------------------------

          M 1 Reply Last reply
          0
          • O Old Gun

            Thank both of you! The second answer is What I wanted,I have test it works. Following is my test code ///--------------------------------------------------------------------------- private void button1_Click(object sender, System.EventArgs e) { Module[] moduleArray; moduleArray = Assembly.GetExecutingAssembly().GetModules(false); //In a simple project with only one module, the module at index // 0 will be the module containing these classes. Module myModule = moduleArray[0]; Type myType; myType = myModule.GetType("WindowsApplication1.Form2"); Form f = (Form) Activator.CreateInstance(myType); f.Show(); } ///------------------------------------------------------------------------- ///-------------------------------------------------------------------------

            M Offline
            M Offline
            Mike Ellison
            wrote on last edited by
            #5

            Hi Richard. I think I misunderstood what you were looking to do. I'm glad you got the information you needed from Baris' post.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups