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.
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.
同意楼上,通过反射来打开你要的打开的那个窗口.参考: 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(); } } }
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. 同意正解
当时为了在子窗体中控制主窗体中的控件,所以在主窗体中实例化打开子窗体时用了下面的代码 sbkh sbkh = new sbkh(this); 如果改为各位提示的方法,用反射来跟据窗体类的名称来打开相应的窗体时,这里的this参数应如果写。才能实现原来的效果,也就是当打开子窗体后,在子窗体中照样能找到主窗体中的控件。 非常感谢上述各位。
Type t = Assembly.GetExecutingAssembly().GetType("zlsmis.sbkh"); object form = Activator.CreateInstance(t); t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null); 将以上代码放入一按钮事件中测试,出错win32异常
发现是这句代码出错 object form = Activator.CreateInstance(t);
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; }
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. 经试验,上面的代码没有问题,很好。是我的子窗体中有几句代码需要改动。谢谢大家
类名是传不了的,如果是传对象,直接Show
{
a.show();
} 编程新手论坛http://freshcoder.5d6d.com/bbs.php
a 对象
你想传类名那窗口的类名不都是form啊 你怎么确定是哪个窗口啊
你问题描述的清楚些 要完成什么功能 才好想办法编程新手论坛http://freshcoder.5d6d.com/bbs.php
{
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.
{
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.
{
//查找当前项目中的所有窗口(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();
}
}
}
{
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.
同意正解
sbkh sbkh = new sbkh(this);
如果改为各位提示的方法,用反射来跟据窗体类的名称来打开相应的窗体时,这里的this参数应如果写。才能实现原来的效果,也就是当打开子窗体后,在子窗体中照样能找到主窗体中的控件。
非常感谢上述各位。
//此代码在子窗体中使用,"FrmMain"是的类名
FrmMain frm = (FrmMain)this.MdiParent;
//把你想要访问的主窗体的控件设置为public
frm.控件名.....
FrmMain frm = (FrmMain)this.MdiParent;
//把你想要访问的主窗体的控件设置为public
frm.控件名.....
object form = Activator.CreateInstance(t);
t.InvokeMember("Show", BindingFlags.InvokeMethod, null, form, null);
将以上代码放入一按钮事件中测试,出错win32异常
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;
}
{
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.
经试验,上面的代码没有问题,很好。是我的子窗体中有几句代码需要改动。谢谢大家