Mdi的子窗口如何关闭?

NewSun99 2003-10-30 06:37:00
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MdiDemo
{
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.Container components = null;

public frmMain()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Open";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "frmMain";
this.Text = "frmMain";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
frmChild pForm = new frmChild();
pForm.MdiParent = this;
pForm.Show();
}
}
}
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dotnba 2003-11-12
  • 打赏
  • 举报
回复
修改构造函数
public frmChild(Form frmMdiParent)
{
InitializeComponent();
//设置前提条件
if(true)
{
this.Close();
this.Dispose();
return;
}
this.MdiParent = frmMdiParent;
this.Show();
}
znull 2003-11-12
  • 打赏
  • 举报
回复
if(false)
{
this.Dispose();
}
else
{
this.Show();
}
去掉后,就可以关闭自己了....
NewSun99 2003-11-03
  • 打赏
  • 举报
回复
GO ON
styker 2003-10-30
  • 打赏
  • 举报
回复
你在Load事件中关闭窗口时可能会导致加载窗口的一些事件失去窗口的句柄。
我估计是这样的。
这里,我一般会改一种写法,其他的办法我也在学习中。
NewSun99 2003-10-30
  • 打赏
  • 举报
回复
如上,我有两个窗体类:frmMain,frmChild,
我想在frmChild的Load事件里关闭她自己,出错。
未处理的"System.InvalidOperationException"类型的异常出现在system.windows.forms.dll中。
其他信息:执行CreateHandle() 时无法调用Close()

当然实际的情况可能是在Load事件里判断一些条件,然后再确定是否关闭。
请问如何解决
NewSun99 2003-10-30
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MdiDemo
{
public class frmChild : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public frmChild()
{
InitializeComponent();
if(false)
{
this.Dispose();
}
else
{
this.Show();
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
//
// frmChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "frmChild";
this.Text = "frmChild";
this.Load += new System.EventHandler(this.frmChild_Load);

}
#endregion

private void frmChild_Load(object sender, System.EventArgs e)
{
this.Close();
}
}
}

110,538

社区成员

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

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

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