现有WINFORM程序中TreeView,包含如
tree1
    tree11(Tag:Form1)
    tree12(Tag:Form2)
    tree13(Tag:Form3)
    tree14(Tag:Form4)
tree2
    tree21(Tag:Form5)
    tree22(Tag:Form6)
    tree23(Tag:Form7)
……
现在想每当用户点击某个子节点(如tree11)的时候就根据它的TAG属性实例化一个对象
如Form1 myForm = new Form1();如果节点不多的话还可以逐个进行Case判断后再决定实例化哪个
但是节点多达N个的时候(非数据库读取)就不太方便了

解决方案 »

  1.   

    我也遇到了这个问题,想问楼主一下,仅实现一级NODE的双击事件怎么做。比如在你这里只实现TREE11--TREE14  的双击事件  而不是根NODE的双击事件!谢谢
      

  2.   

    public Form[] frm123;this.frm123=new Form[n];
    frm123[0]=new yourfom1();
    frm123[n-1]=new yourfomN();
    frm123[n-1].ShowDialog();
      

  3.   

    tree1
        tree11(Tag:Form1)
        tree12(Tag:Form2)
        tree13(Tag:Form3)
        tree14(Tag:Form4)
    tree2
        tree21(Tag:Form5)
        tree22(Tag:Form6)
        tree23(Tag:Form7)
    当用户点击的时候可以用treeView1.GetNodeAt(e.X,e.Y).Tag取得当前节点的TAG
    比如取得Form1
    这个时候我想实例化一个Form1类,不知道可不可以?
      

  4.   

    通过反射实现:节点的tag只是存储窗口类字符串,根据该字符串利用反射创建该窗口
    String FormName = (String)(treeView1.SelectedNode.Tag); //"Form1"
       Type type = Type.GetType(FormName);
       System.Reflection.Assembly a = System.Reflection.Assembly.GetAssembly(type);
       Object obj = a.CreateInstance(FormName);
       type.InvokeMember("Show",System.Reflection.BindingFlags.InvokeMethod,null,obj,null);
      

  5.   

    magicsnake(北极狐) 
    Type type = Type.GetType(FormName); 为null,还有什么办法?
      

  6.   

    多谢各位
    已经解决了
    方法如下:
      private static Object objclass;
            public static object GetInstance(string strClassName)
            {           
                try
                {                
                    objclass = System.Activator.CreateInstance(System.Type.GetType(strClassName));
                }
                catch(Exception exmsg)
                {
                    throw new Exception( "动态创建实例失败 \n\n=> " + strClassName , exmsg);                
                }       
                return objclass;
            }
    注意传入字符串的时候要把命名空间一起写进去
    比如:
    (System.Windows.Forms.UserControl)InstanceUserControl.GetInstance("Efuture.ESilkRoad."+treeView1.GetNodeAt(e.X,e.Y).Tag.ToString())