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
  1. Home
  2. General Programming
  3. C#
  4. How to call a form if you know the form name only as a string variable >

How to call a form if you know the form name only as a string variable >

Scheduled Pinned Locked Moved C#
databasewinformshelptutorial
5 Posts 3 Posters 1 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.
  • U Offline
    U Offline
    unitecsoft
    wrote on last edited by
    #1

    Hi I have a small problem here which I am trying to solve . I have many Windows Forms in my application which I want to call and show each one based on its name , the name is stored in a database table as a string variable , this table is changeable by the user . I repeat the only thing I know about the Form is its name as a string variable . assume no parameters are to be passed to the Form > any Ideas Thanks We will Either find a way , or make one .

    J G 2 Replies Last reply
    0
    • U unitecsoft

      Hi I have a small problem here which I am trying to solve . I have many Windows Forms in my application which I want to call and show each one based on its name , the name is stored in a database table as a string variable , this table is changeable by the user . I repeat the only thing I know about the Form is its name as a string variable . assume no parameters are to be passed to the Form > any Ideas Thanks We will Either find a way , or make one .

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Is the form you're looking for already opened? If so, you could call Application.OpenForms and find the opened form based on the name. If your form isn't opened and you need to instantiate and display one based on the name, you can use Reflection. In particular, something like GetType.Assembly.GetType("TheNameGoesHere")

      Life, family, faith: Give me a visit. From my latest post: "And you think, 'To keep my anti-Judaic theology alive I must reinterpret this verse too as being a blessing for Christians and not for Jews. I know it strains all manner of principles of interpretation. I don’t read the newspaper this sloppily, but, man, I have a theology to defend.'" Judah Himango

      U 1 Reply Last reply
      0
      • U unitecsoft

        Hi I have a small problem here which I am trying to solve . I have many Windows Forms in my application which I want to call and show each one based on its name , the name is stored in a database table as a string variable , this table is changeable by the user . I repeat the only thing I know about the Form is its name as a string variable . assume no parameters are to be passed to the Form > any Ideas Thanks We will Either find a way , or make one .

        G Offline
        G Offline
        Gareth H
        wrote on last edited by
        #3

        unitecsoft, Reflection[^]

        Regards, Gareth. (FKA gareth111)

        1 Reply Last reply
        0
        • J Judah Gabriel Himango

          Is the form you're looking for already opened? If so, you could call Application.OpenForms and find the opened form based on the name. If your form isn't opened and you need to instantiate and display one based on the name, you can use Reflection. In particular, something like GetType.Assembly.GetType("TheNameGoesHere")

          Life, family, faith: Give me a visit. From my latest post: "And you think, 'To keep my anti-Judaic theology alive I must reinterpret this verse too as being a blessing for Christians and not for Jews. I know it strains all manner of principles of interpretation. I don’t read the newspaper this sloppily, but, man, I have a theology to defend.'" Judah Himango

          U Offline
          U Offline
          unitecsoft
          wrote on last edited by
          #4

          can you be more clear about the second case , Create an instance of a form by the form name as string only ? thanks

          J 1 Reply Last reply
          0
          • U unitecsoft

            can you be more clear about the second case , Create an instance of a form by the form name as string only ? thanks

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            I pointed you in the right direction already: Type theFormWeAreLookingFor = this.GetType().Assembly.GetType("TheNameGoesHere") From there, you've got a Type. Look for the GetConstructor method, and call invoke on that. Voila! You have yourself a form. Read up on Reflection if you need more -- Gareth linked to the MSDN article in his reply to you.

            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