请教,以窗体的名字作为参数打开此窗体可行吗?如行,应怎样做?如:
void   open(form   a)   
  {   
        a a=new a();
        a.show();   
  }  

解决方案 »

  1.   

    你要传类名还是传对象?
    类名是传不了的,如果是传对象,直接Show
      

  2.   

    void  open(form  a)  
      {  
            a.show(); 
      } 编程新手论坛http://freshcoder.5d6d.com/bbs.php
      

  3.   

    form 类
    a 对象
    你想传类名那窗口的类名不都是form啊 你怎么确定是哪个窗口啊
    你问题描述的清楚些 要完成什么功能 才好想办法编程新手论坛http://freshcoder.5d6d.com/bbs.php
      

  4.   

    估计楼主是想通过参数来决定show哪个窗口,可以写几个函数,然后传个委托过去(随便想到的,没确认可不可行,仅供参考。)
      

  5.   

    public void Open(string name)
            {
                Type t = Assembly.GetExecutingAssembly().GetType(name);
                object form = Activator.CreateInstance(t);
                t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null);
            }//Make sure the input parm format is "WindowsApplication1.Form2" with the fullpath.
      

  6.   

    public void Open(string name)
            {
                Type t = Assembly.GetExecutingAssembly().GetType(name);
                object form = Activator.CreateInstance(t);
                t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null);
            }//Make sure the input parm format is "WindowsApplication1.Form2" with the fullpath.
      

  7.   

    同意楼上,通过反射来打开你要的打开的那个窗口.参考:        void Open(string name)
            {
                //查找当前项目中的所有窗口(Form)的子类
                foreach (var item in this.GetType().Assembly.GetTypes())
                {
                    if (item.IsSubclassOf(typeof(Form))&&item.Name==name)
                    {
                        //找到窗口子类并且为指定名称,那么使用基类的模板打开它.
                        Form v=item.GetConstructor(Type.EmptyTypes).Invoke( null) as Form;
                        v.Show();
                    }
                }
            }
      

  8.   

    C# codepublic void Open(string name)
            {
                Type t = Assembly.GetExecutingAssembly().GetType(name);
                object form = Activator.CreateInstance(t);
                t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null);
            }//Make sure the input parm format is "WindowsApplication1.Form2" with the fullpath.
    同意正解
      

  9.   

    当时为了在子窗体中控制主窗体中的控件,所以在主窗体中实例化打开子窗体时用了下面的代码
     sbkh sbkh = new sbkh(this);
    如果改为各位提示的方法,用反射来跟据窗体类的名称来打开相应的窗体时,这里的this参数应如果写。才能实现原来的效果,也就是当打开子窗体后,在子窗体中照样能找到主窗体中的控件。
    非常感谢上述各位。
      

  10.   


    //此代码在子窗体中使用,"FrmMain"是的类名
    FrmMain frm = (FrmMain)this.MdiParent;
    //把你想要访问的主窗体的控件设置为public
    frm.控件名.....
      

  11.   

    //此代码在子窗体中使用,"FrmMain"是主窗体的类名
    FrmMain frm = (FrmMain)this.MdiParent;
    //把你想要访问的主窗体的控件设置为public
    frm.控件名.....
      

  12.   

                Type t = Assembly.GetExecutingAssembly().GetType("zlsmis.sbkh");
                object form = Activator.CreateInstance(t);
                t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null);
    将以上代码放入一按钮事件中测试,出错win32异常
      

  13.   

    发现是这句代码出错            object form = Activator.CreateInstance(t);
      

  14.   


    public static int CreatNewObject(string className,object[] args,out object newObject)
    {
    int rtn=-1;
    newObject=null; try
    {
    Type tp=Type.GetType(className);
    //根据类型生成对象
    newObject=Activator.CreateInstance(tp,args);
    rtn=0;
    }
    catch(TypeLoadException ex)
    {
    Console.WriteLine(ex.Message);
    }
    catch(Exception ex)
    {
    errMsg=ex.Message;
    }
    return rtn;
    }
      

  15.   

    C# code public void Open(string name) 
            { 
                Type t = Assembly.GetExecutingAssembly().GetType(name); 
                object form = Activator.CreateInstance(t); 
                t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null); 
            } //Make sure the input parm format is "WindowsApplication1.Form2" with the fullpath. 
    经试验,上面的代码没有问题,很好。是我的子窗体中有几句代码需要改动。谢谢大家