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. Runtime form Object Reference

Runtime form Object Reference

Scheduled Pinned Locked Moved C#
questiondatabase
6 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.
  • V Offline
    V Offline
    vaibhavnvag
    wrote on last edited by
    #1

    I want to display a form, which(name of form) is stored into a table.After firing query to that table I am getting appropriate value (e.g "frmSTART") into a string variable(myfrm = "frmSTART"), now how can I treat a string variable value as an object? So that I can write: myfrm nSTART = new myfrm (); nSTART.Show();

    Hallo

    M 1 Reply Last reply
    0
    • V vaibhavnvag

      I want to display a form, which(name of form) is stored into a table.After firing query to that table I am getting appropriate value (e.g "frmSTART") into a string variable(myfrm = "frmSTART"), now how can I treat a string variable value as an object? So that I can write: myfrm nSTART = new myfrm (); nSTART.Show();

      Hallo

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, What you asked for is not possible! .) If you know all the possible class names during design time, you simple could make a switch case over the returned string.

      string myform = actual_returned_string;

      switch(myform)
      {
      case "test1":
      {
      MyTest1 t1 = new MyTest1();
      break;
      }

      case "test2":
      {
      MyTest2 t1 = new MyTest2();
      break;
      }
      
      default:
      break;
      

      }

      .) If you have to make it more dynamic, then you have to look into reflection. Therefore you should know the namespace of youre formclasses. You could use a method like this, which returns you a Form to Show.

      using System.Reflection;

      public System.Windows.Forms.Form GetFormByName(string _name)
      {
      Type t = Type.GetType(_name);
      if (t != null)
      {
      ConstructorInfo c = t.GetConstructor(Type.EmptyTypes);
      Object o = c.Invoke(new Object[0]);
      return o as System.Windows.Forms.Form;
      }
      else
      {
      return null;
      }
      }

      And you can call it like that:

      string namespace = //???
      string myform = namespace+actual_returned_string;

      System.Windows.Forms.Form actForm = GetFormByName(myform);

      Hope it works for you! All the best, Martin

      C 1 Reply Last reply
      0
      • M Martin 0

        Hello, What you asked for is not possible! .) If you know all the possible class names during design time, you simple could make a switch case over the returned string.

        string myform = actual_returned_string;

        switch(myform)
        {
        case "test1":
        {
        MyTest1 t1 = new MyTest1();
        break;
        }

        case "test2":
        {
        MyTest2 t1 = new MyTest2();
        break;
        }
        
        default:
        break;
        

        }

        .) If you have to make it more dynamic, then you have to look into reflection. Therefore you should know the namespace of youre formclasses. You could use a method like this, which returns you a Form to Show.

        using System.Reflection;

        public System.Windows.Forms.Form GetFormByName(string _name)
        {
        Type t = Type.GetType(_name);
        if (t != null)
        {
        ConstructorInfo c = t.GetConstructor(Type.EmptyTypes);
        Object o = c.Invoke(new Object[0]);
        return o as System.Windows.Forms.Form;
        }
        else
        {
        return null;
        }
        }

        And you can call it like that:

        string namespace = //???
        string myform = namespace+actual_returned_string;

        System.Windows.Forms.Form actForm = GetFormByName(myform);

        Hope it works for you! All the best, Martin

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        I don't know why you got voted a 2 for that - I thought the reflection idea was sound.


        Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

        M E 2 Replies Last reply
        0
        • C Colin Angus Mackay

          I don't know why you got voted a 2 for that - I thought the reflection idea was sound.


          Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello,

          Colin Angus Mackay wrote:

          I don't know why you got voted a 2 for that - I thought the reflection idea was sound.

          I have also no idea! It would also be interesting for me to know way, cause I'm using reflection in my application. So thank's for youre statement! All the best, Martin

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            I don't know why you got voted a 2 for that - I thought the reflection idea was sound.


            Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

            E Offline
            E Offline
            ednrgc
            wrote on last edited by
            #5

            Probably because the answer was above the person's comprehension.

            C 1 Reply Last reply
            0
            • E ednrgc

              Probably because the answer was above the person's comprehension.

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              ednrgc wrote:

              Probably because the answer was above the person's comprehension.

              If someone gave me an answer that I didn't understand then I'd follow it up with a request for clarification. Oh, Well.... :sigh:


              Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

              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