---如何能在C#中一次性关闭所有的MDI子窗体---

ChitchatPrince 2004-09-12 04:54:28
在MDI主窗体中,如何一次关闭所有MDI子窗体?
...全文
1747 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangshaojun 2004-09-27
  • 打赏
  • 举报
回复
foreach(Form _form in this.MdiChildren)
{
_form.Close();
}
wangrong001 2004-09-13
  • 打赏
  • 举报
回复
Form[] frmList = this.MdiChildren;
foreach(Form frm in frmList)
{
frm.Close();
}
北京的雾霾天 2004-09-13
  • 打赏
  • 举报
回复
private void CloseAllChild()
{
for ( int i = 0; i < this.MdiChildren.Length ; i++ )
{
(this.MdiChildren[ i ] as Form).Close();
}
}
北京的雾霾天 2004-09-13
  • 打赏
  • 举报
回复
这样用:
private void CloseAllChild()
{
for ( int i = 0; i < this.MdiChildren.Length ; i++ )
{
this.Text = "正在关闭: " + (this.MdiChildren[ i ] as Form).Text ;
(this.MdiChildren[ i ] as Form).Close();
this.Text = "已关闭: " + (this.MdiChildren[ i ] as Form).Text ;
}
this.Text = "已关闭所有的子窗口";
}
yxred 2004-09-12
  • 打赏
  • 举报
回复
阿K
Form[] frmList = this.MdiChildren;
foreach(Form frm in frmList)
{
frm.Close();
}
hxhbluestar 2004-09-12
  • 打赏
  • 举报
回复
遍历所有的子窗体,只要是活动的,就关闭!
sjzwinfor 2004-09-12
  • 打赏
  • 举报
回复
Form activeChild = this.ActiveMdiChild;

while (activeChild != null)
{
activeChild.Close();
activeChild = this.ActiveMdiChild;
}
huangya0 2004-09-12
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestMDI
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4});
this.menuItem1.Text = "Form";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Form1";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "Form2";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "CloseAll";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;

}

private void menuItem3_Click(object sender, System.EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm3.MdiParent = this;
}

private void menuItem4_Click(object sender, System.EventArgs e)
{
Form[] frmList = this.MdiChildren;
foreach(Form frm in frmList)
{
frm.Close();
}
}
}
}

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧