日期:2014-05-20  浏览次数:20790 次

动态调用窗体方法请教。
一个主窗体和N个子窗体
主窗体通过调用子窗体的   shwo子窗体名()   方法来打开子窗体,但是主窗体有很多菜单项,每次调用的代码都几乎相同,看能不能精简一下。我先给出我的代码:

主窗口调用代码(几乎每个调用都是这么写的):
//权限设置
private   void   MSys_Power_Click(object   sender,   System.EventArgs   e)
{
Form   frm   =   null;
for   (int   i   =   0;   i   <   this.MdiChildren.Length;   i++)
{
if   (this.MdiChildren[i].Name   ==   "RolePowerManage ")
{
frm   =   this.MdiChildren[i];
break;
}
}
if   (frm   ==   null)
{
this.stBarMain.Panels[0].Text   =   "正在加载权限设置窗体... ";
this.Refresh();
SystemSet.RolePowerManage.ShowRolePowerManage(this);
this.stBarMain.Panels[0].Text   =   "就绪 ";
}
else
{
frm.BringToFront();
frm.Activate();
}
}
子窗口ShowRolePowerManage()方法:
public   static   void   ShowRolePowerManage(Form   FName)
{
Form   frmTip   =   LoadTip.ShowTipForm(FName, "正在加载窗体... ");
RolePowerManage   frm   =   new   RolePowerManage();
frm.MdiParent   =   FName   ;
frm.Show();
frmTip.Close();
}
子窗口显示的方法我都是:show+窗口名

我在想,能不能把主窗口调用的代码写成一个方法,我知道要用反射,可惜学艺不精,请高手帮忙

------解决方案--------------------
Type type = Type.GetType( "窗体的完整类名(MyNameSpace.MyForm) ");
Form objFrm = System.Activator.CreateInstance(type);
objFrm.show();

反射