MDI 父窗体menustrip,控制当前激活的子窗体 RT谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看这个helper类能不能对你有点帮助:public class FromUtil { /// <summary> /// 在MDI窗体中寻找ChildForm,如果childform已经存在则激活为当前子窗体,如果子窗体不存在则建立 /// </summary> /// <param name="MDIForm"></param> /// <param name="ChildForm"></param> public static void ActionChildForm(System.Windows.Forms.Form MDIForm, System.Windows.Forms.Form ChildForm) { foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren) { if (ChildForm.GetType() == f.GetType()) { ChildForm = null; f.WindowState = System.Windows.Forms.FormWindowState.Maximized; f.Show(); f.Activate(); //break; return; } } ChildForm.WindowState = System.Windows.Forms.FormWindowState.Maximized; ChildForm.MdiParent = MDIForm; ChildForm.Show(); } /// <summary> /// 在MDI窗体中寻找ChildForm,如果childform已经存在则激活为当前子窗体,如果子窗体不存在则建立 /// 同时关闭其它子窗体 /// </summary> /// <param name="MDIForm"></param> /// <param name="ChildForm"></param> public static void ActionSingleChildForm(System.Windows.Forms.Form MDIForm, System.Windows.Forms.Form ChildForm) { foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren) { if (ChildForm.GetType() == f.GetType()) { ChildForm = null; f.WindowState = System.Windows.Forms.FormWindowState.Maximized; f.Show(); f.Activate(); } else { f.Close(); } } } } if (this.ActiveMdiChild.Name == "dp") { dplanmain f = (dp)this.ActiveMdiChild; } 可不可以不要判断name,弄个通用的写法(不管那个子窗体都可适用) 通用的写法,那你的“不管哪个子窗体”都有“通用”的操作方法了?一个类继承下来的?那就直接把ActiveMdiChild转换成父类,调用父类的方法喽。 foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren) 可能找到你的所有窗体,你可以根据条件激活你可以参 http://www.cnblogs.com/wts/archive/2007/03/15/675806.html Form1 fm = (Form1)this.ActiveMdiChild ;然后操作fm就是了 举个例子有一个主窗口 Mainform.cs两个子窗口 Form1和 Form2 Mainform.cs//用全局变量来控制static class Common{ public static Form1 sform1 = null; public static Form2 sform2 = null;}//在Mainform.cs中 添加menustrip//并创建两个子菜单ViewMenu和WindowsMenu//主菜单menuStrip1中将MdiWindowListItem设置成WindowsMenu this.menuStrip1.MdiWindowListItem = this.windowToolStripMenuItem;//在ViewMenu中添加2个子选项form1ToolStripMenuItem和form2ToolStripMenuItem//并且添加click事件 this.form1ToolStripMenuItem.Click += new System.EventHandler(this.form1ToolStripMenuItem_Click); this.form2ToolStripMenuItem.Click += new System.EventHandler(this.form2ToolStripMenuItem_Click);//这里写一个form1ToolStripMenuItem的click事件,form2ToolStripMenuItem和它相同做法 private void form1ToolStripMenuItem_Click(object sender, EventArgs e) { if (Common.sform1.Visible == true) { Common.sform1.Visible = false; } else { Common.sform1.ReVisible(); } }//然后给Mainform追加一个FormClosing事件和Load事件//创建子窗体 private void Mainform_Load(object sender, EventArgs e) { Common.sform1 = new Form1(); Common.sform1.MdiParent = this; Common.sform2 = new Form2(); Common.sform2.MdiParent = this; }//关闭所有子窗体 private void Mainform_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = false; }//————————————————————————————————————————————————//Form1//添加引用using System.Reflection;//显示隐藏的子窗体 public void ReVisible() { FieldInfo fi = typeof(Form).GetField("closeChild", BindingFlags.Instance | BindingFlags.NonPublic); fi.SetValue(this, CloseReason.None); this.Visible = true; }//formClosing事件,这个关闭只相当于将子窗体隐藏 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; if (e.CloseReason == CloseReason.UserClosing) { this.Visible = false; } }//这样你可以控制Common.sform1 来控制子窗体//并且可以通过菜单ViewMenu来控制子窗体的打开和关闭,//通过菜单WindowsMenu来控制打开的子窗体,哪个前端显示//以上是部分代码,希望对你有帮助 FieldInfo fi = typeof(Form).GetField("closeChild", BindingFlags.Instance | BindingFlags.NonPublic);这里写错了应该是GetField("closeReason"对不起写太快了没检查 谁有c#写的采购管理的系统源码 关于WWF(Windows Workflow Fundation)相关的WCF工作流服务库 高手啊!快进——如何提高图片在报表中的打印质量 关于调用.net命令编译生成执行文件的问题 C#中如何实现收取Gmail邮件. 有谁知道做投票调查系统的思路吗!?谢谢各位仁兄啦! 用C#怎么写一个自定义的序列化和反序列化的类 winform下,datagrid结合扫描枪定位问题 C++中的unsigned char *参数转到C#中对应那种数据类型?怎么调用? WCF 双工通信 C#命令钮缺省选中是哪个属性 c#结构指针的问题,急~~~~
public class FromUtil
{ /// <summary>
/// 在MDI窗体中寻找ChildForm,如果childform已经存在则激活为当前子窗体,如果子窗体不存在则建立
/// </summary>
/// <param name="MDIForm"></param>
/// <param name="ChildForm"></param>
public static void ActionChildForm(System.Windows.Forms.Form MDIForm, System.Windows.Forms.Form ChildForm)
{
foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren)
{
if (ChildForm.GetType() == f.GetType())
{
ChildForm = null;
f.WindowState = System.Windows.Forms.FormWindowState.Maximized;
f.Show();
f.Activate();
//break;
return;
}
}
ChildForm.WindowState = System.Windows.Forms.FormWindowState.Maximized;
ChildForm.MdiParent = MDIForm;
ChildForm.Show(); }
/// <summary>
/// 在MDI窗体中寻找ChildForm,如果childform已经存在则激活为当前子窗体,如果子窗体不存在则建立
/// 同时关闭其它子窗体
/// </summary>
/// <param name="MDIForm"></param>
/// <param name="ChildForm"></param>
public static void ActionSingleChildForm(System.Windows.Forms.Form MDIForm, System.Windows.Forms.Form ChildForm)
{
foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren)
{
if (ChildForm.GetType() == f.GetType())
{
ChildForm = null;
f.WindowState = System.Windows.Forms.FormWindowState.Maximized;
f.Show();
f.Activate();
}
else
{
f.Close();
}
}
}
}
{
dplanmain f = (dp)this.ActiveMdiChild;
}
可不可以不要判断name,弄个通用的写法(不管那个子窗体都可适用)
可能找到你的所有窗体,你可以根据条件激活
你可以参 http://www.cnblogs.com/wts/archive/2007/03/15/675806.html
然后操作fm就是了
有一个主窗口 Mainform.cs
两个子窗口 Form1和 Form2 Mainform.cs//用全局变量来控制
static class Common
{
public static Form1 sform1 = null;
public static Form2 sform2 = null;
}
//在Mainform.cs中 添加menustrip
//并创建两个子菜单ViewMenu和WindowsMenu
//主菜单menuStrip1中将MdiWindowListItem设置成WindowsMenu this.menuStrip1.MdiWindowListItem = this.windowToolStripMenuItem;//在ViewMenu中添加2个子选项form1ToolStripMenuItem和form2ToolStripMenuItem
//并且添加click事件
this.form1ToolStripMenuItem.Click += new System.EventHandler(this.form1ToolStripMenuItem_Click);
this.form2ToolStripMenuItem.Click += new System.EventHandler(this.form2ToolStripMenuItem_Click);
//这里写一个form1ToolStripMenuItem的click事件,form2ToolStripMenuItem和它相同做法
private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Common.sform1.Visible == true)
{
Common.sform1.Visible = false;
}
else
{
Common.sform1.ReVisible();
}
}
//然后给Mainform追加一个FormClosing事件和Load事件
//创建子窗体
private void Mainform_Load(object sender, EventArgs e)
{
Common.sform1 = new Form1();
Common.sform1.MdiParent = this;
Common.sform2 = new Form2();
Common.sform2.MdiParent = this;
}
//关闭所有子窗体
private void Mainform_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
}//————————————————————————————————————————————————
//Form1
//添加引用
using System.Reflection;
//显示隐藏的子窗体 public void ReVisible() {
FieldInfo fi = typeof(Form).GetField("closeChild",
BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, CloseReason.None);
this.Visible = true;
}
//formClosing事件,这个关闭只相当于将子窗体隐藏 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
if (e.CloseReason == CloseReason.UserClosing)
{
this.Visible = false;
}
}
//这样你可以控制Common.sform1 来控制子窗体
//并且可以通过菜单ViewMenu来控制子窗体的打开和关闭,
//通过菜单WindowsMenu来控制打开的子窗体,哪个前端显示
//以上是部分代码,希望对你有帮助
BindingFlags.Instance | BindingFlags.NonPublic);
这里写错了应该是GetField("closeReason"
对不起写太快了没检查