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. Solving (string) and creating the screen - Ex.: CreateScreen("screenTest")

Solving (string) and creating the screen - Ex.: CreateScreen("screenTest")

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 Posts 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Friends, I hope you can help me. I am developing a new project and this project I have several screens. Well, I would like to create a screen through her name and that name is the same class name of the screen. Example: I create a new screen, and put her name as formCadastro. Now, in my base form, I would go to a function the string value "formCadastro" function and open the screen with the same name. Is it possible? I tried to work a little with the System.Windows.Forms.Form class but I could not ... Thank you for your help. Success for All

    N V R 3 Replies Last reply
    0
    • L Lost User

      Friends, I hope you can help me. I am developing a new project and this project I have several screens. Well, I would like to create a screen through her name and that name is the same class name of the screen. Example: I create a new screen, and put her name as formCadastro. Now, in my base form, I would go to a function the string value "formCadastro" function and open the screen with the same name. Is it possible? I tried to work a little with the System.Windows.Forms.Form class but I could not ... Thank you for your help. Success for All

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Like this[^] ?


      I know the language. I've read a book. - _Madmatt

      L 1 Reply Last reply
      0
      • L Lost User

        Friends, I hope you can help me. I am developing a new project and this project I have several screens. Well, I would like to create a screen through her name and that name is the same class name of the screen. Example: I create a new screen, and put her name as formCadastro. Now, in my base form, I would go to a function the string value "formCadastro" function and open the screen with the same name. Is it possible? I tried to work a little with the System.Windows.Forms.Form class but I could not ... Thank you for your help. Success for All

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        Have you tried a switch statement?

        switch(inputname){
        case "Screen1": Screen1 s1 = new Screen1();
        s1.ShowDialog();
        break;

        case "Screen2": Screen2 s2 = new Screen2();
        s2.ShowDialog();
        break;

        // continue

        default: // add a default situation.
        break;
        }

        V.

        L 1 Reply Last reply
        0
        • V V 0

          Have you tried a switch statement?

          switch(inputname){
          case "Screen1": Screen1 s1 = new Screen1();
          s1.ShowDialog();
          break;

          case "Screen2": Screen2 s2 = new Screen2();
          s2.ShowDialog();
          break;

          // continue

          default: // add a default situation.
          break;
          }

          V.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Friend, this will not work. There are many screens, and would like to develop a generic method. Thanks!

          1 Reply Last reply
          0
          • N Not Active

            Like this[^] ?


            I know the language. I've read a book. - _Madmatt

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Friend, it would be nearly so. But I wanted something simpler. There is to create a container of the screens? I am studying the example. Thanks

            1 Reply Last reply
            0
            • L Lost User

              Friends, I hope you can help me. I am developing a new project and this project I have several screens. Well, I would like to create a screen through her name and that name is the same class name of the screen. Example: I create a new screen, and put her name as formCadastro. Now, in my base form, I would go to a function the string value "formCadastro" function and open the screen with the same name. Is it possible? I tried to work a little with the System.Windows.Forms.Form class but I could not ... Thank you for your help. Success for All

              R Offline
              R Offline
              rhuiden
              wrote on last edited by
              #6

              I would use something like this...

              private void CreateScreen(string FullName, params object\[\] args)
              

              {
                  Type t ;
                  Form f;
                 
                  t = Type.GetType(FullName);
                 
                  if (t != null) {
                      f = Activator.CreateInstance(t, args) as Form;
                      if (f != null) {
                          f.Show();
                      }
                  }
              }

              This would also allow you to pass arguments to the constructor. You may also have to pass the Full name of the Form including the Namespace. I don't remember how Type.GetType() works. Edit: Sorry, forgot I was in C#.

              modified on Monday, February 8, 2010 11:02 AM

              L 1 Reply Last reply
              0
              • R rhuiden

                I would use something like this...

                private void CreateScreen(string FullName, params object\[\] args)
                

                {
                    Type t ;
                    Form f;
                   
                    t = Type.GetType(FullName);
                   
                    if (t != null) {
                        f = Activator.CreateInstance(t, args) as Form;
                        if (f != null) {
                            f.Show();
                        }
                    }
                }

                This would also allow you to pass arguments to the constructor. You may also have to pass the Full name of the Form including the Namespace. I don't remember how Type.GetType() works. Edit: Sorry, forgot I was in C#.

                modified on Monday, February 8, 2010 11:02 AM

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Perfect! Thanks Brother Sucess!

                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