Launching Forms Dynamically
-
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?
-
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?
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 theTreeView
. This will allow you to interogate theTreeNode
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 -
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 theTreeView
. This will allow you to interogate theTreeNode
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 ArticlesI'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?
-
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?
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
-
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
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?
-
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?