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. Launching Forms Dynamically

Launching Forms Dynamically

Scheduled Pinned Locked Moved C#
helpquestion
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.
  • S Offline
    S Offline
    SignMan359
    wrote on last edited by
    #1

    I'm writing a program that will populate a TreeView with a list of all the "programs" that can be run from within my program. These "programs" will actually just be MDI children. The name of the MDI child form will be loaded into the TreeMenu.SelectedNode.Tag property. My problem is to be able to launch a form dynamically based on which item they double-click on. Any ideas? --In a world without fences, who needs Gates?

    N 1 Reply Last reply
    0
    • S SignMan359

      I'm writing a program that will populate a TreeView with a list of all the "programs" that can be run from within my program. These "programs" will actually just be MDI children. The name of the MDI child form will be loaded into the TreeMenu.SelectedNode.Tag property. My problem is to be able to launch a form dynamically based on which item they double-click on. Any ideas? --In a world without fences, who needs Gates?

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      SignMan359 wrote: My problem is to be able to launch a form dynamically based on which item they double-click on. You can handle the AfterSelect event from the TreeView. This will allow you to interogate the TreeNode that issued the event. Something like the following will do it:

      tv = new TreeView();
      tv.AfterSelect += new TreeViewEventHandler(tv\_AfterSelect);
      
      private void tv\_AfterSelect(object sender, TreeViewEventArgs tvea)
      {
      	TreeNode node = tvea.Node;
      	if(node != null)
      	{
      		//Access the Tag property of node here.
      	}
      }
      

      - Nick Parker
      My Blog | My Articles

      S 1 Reply Last reply
      0
      • N Nick Parker

        SignMan359 wrote: My problem is to be able to launch a form dynamically based on which item they double-click on. You can handle the AfterSelect event from the TreeView. This will allow you to interogate the TreeNode that issued the event. Something like the following will do it:

        tv = new TreeView();
        tv.AfterSelect += new TreeViewEventHandler(tv\_AfterSelect);
        
        private void tv\_AfterSelect(object sender, TreeViewEventArgs tvea)
        {
        	TreeNode node = tvea.Node;
        	if(node != null)
        	{
        		//Access the Tag property of node here.
        	}
        }
        

        - Nick Parker
        My Blog | My Articles

        S Offline
        S Offline
        SignMan359
        wrote on last edited by
        #3

        I'm sorry, maybe I confused the issue. I'm not having a problem with the TreeView. Here is where my problem is: Let's say you have a form named HelloWorld, you would get that for to show up by typing something like: HelloWorld newForm = new HelloWorld(); newForm.Show(); My problem is how do I get the same result of it was more like this: string formName = "HelloWorld"; formName newForm = new formName; Yes, I know this is totally invalid syntax, but I think it explains what I am trying to do. does anyody have any idea how to do this correctly? --In a world without fences, who needs Gates?

        A 1 Reply Last reply
        0
        • S SignMan359

          I'm sorry, maybe I confused the issue. I'm not having a problem with the TreeView. Here is where my problem is: Let's say you have a form named HelloWorld, you would get that for to show up by typing something like: HelloWorld newForm = new HelloWorld(); newForm.Show(); My problem is how do I get the same result of it was more like this: string formName = "HelloWorld"; formName newForm = new formName; Yes, I know this is totally invalid syntax, but I think it explains what I am trying to do. does anyody have any idea how to do this correctly? --In a world without fences, who needs Gates?

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          In this example I am storing the Mdi's type in the node's tag. You could set the text of ur node to be the mdi form's classname instead if it were a valid solution. Type type=Type.GetType((string)selectedTreeNode.Tag); Form mdiChild=(Form)Activator.CreateInstance(type); P.D. Activator.CreateInstance() can pass arguments to the object to instantiate constructor if needed. Just check the msdn information about the different overloads

          S 1 Reply Last reply
          0
          • A Anonymous

            In this example I am storing the Mdi's type in the node's tag. You could set the text of ur node to be the mdi form's classname instead if it were a valid solution. Type type=Type.GetType((string)selectedTreeNode.Tag); Form mdiChild=(Form)Activator.CreateInstance(type); P.D. Activator.CreateInstance() can pass arguments to the object to instantiate constructor if needed. Just check the msdn information about the different overloads

            S Offline
            S Offline
            SignMan359
            wrote on last edited by
            #5

            I've tried the above. I have the name of the class (and therefore the form) in the .tag property. However, when I try to run this code, it leaves the type variable null. I'm at a loss. I'm pretty new to C# and .Net, so please forgive my ignorance if this is a simple problem. --In a world without fences, who needs Gates?

            S 1 Reply Last reply
            0
            • S SignMan359

              I've tried the above. I have the name of the class (and therefore the form) in the .tag property. However, when I try to run this code, it leaves the type variable null. I'm at a loss. I'm pretty new to C# and .Net, so please forgive my ignorance if this is a simple problem. --In a world without fences, who needs Gates?

              S Offline
              S Offline
              Skynyrd
              wrote on last edited by
              #6

              Put the whole name of your type, for example: MyApplication.MyObjects.MyType (Check the namespace of ur type.)

              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