为什么在窗体的Load事件响应函数里不能用Close关掉窗体? load事件是窗体的加载事件,当然不能在其中关闭窗体了。不过你可在unload事件中关闭窗体。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是个子窗体;我在工作,所以就加了个子窗体;我这里可以关的呀;using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace WindowsApplication4{/// <summary>/// Form2 的摘要说明。/// </summary>public class Form2 : System.Windows.Forms.Form{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.Container components = null;private bool CheckClosed;public Form2(){ // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 //}/// <summary>/// 清理所有正在使用的资源。/// </summary>protected override void Dispose( bool disposing ){ if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing );}#region Windows Form Designer generated code/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){ // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(480, 273); this.Name = "Form2"; this.Text = "Form2"; this.Load += new System.EventHandler(this.Form2_Load); this.Layout += new System.Windows.Forms.LayoutEventHandler(this.Form2_Layout);}#endregionprivate void Form2_Load(object sender, System.EventArgs e){ //你在这里加入验证;决定CheckClosed this.CheckClosed=true;}private void Form2_Layout(object sender, System.Windows.Forms.LayoutEventArgs e){ if(this.CheckClosed) this.Close();}}} 在Load事件中用这个试一试:Application.Exit();private void Form1_Load(object sender, System.EventArgs e){ int i = 0; if (i == 0) Application.Exit();} 在Form_Resize事件中进行UNLOAD。不过这样,可能有界面一闪的情况 对于Form里的私有数据,可以给出访问的属性,在调用方直接读取或设置其属性。 回poetc,我也按你这样试了一下,真不行,出现异常“执行 CreateHandle() 时无法调用 Close()” 以下代码未经验证public void ShowMe(){ if(条件) this.Close(); else this.Show();}用form1.ShowMe();显示窗体 Heray(Heray) ( ),你把你的代码贴出来;我这里没有任何问题呀. 网站如何记录和显示用户浏览过的资源记录 C#与数据库中的主键问题 身份证验证的C#方法 同样是反射,怎么结果就一样呢? 急啊!! 关于提取中文正规表达式 一个简单问题,怎么解决,文件名与路径混淆 如何使form的边框不显示的同时,任务栏上的缩小窗口能显示ico阿。。。 C#中怎么发送结构体 一个值得讨论的问题 高手赐教(可以多给分)---如何在c#中的picturebox中把一个dib句柄的内容显示出来? 關于Socket的問題.答者有分.多勞多分 请问各位大虾.net的树形目录的控件有吗?
我在工作,所以就加了个子窗体;
我这里可以关的呀;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;namespace WindowsApplication4
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool CheckClosed;public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 273);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.Layout += new System.Windows.Forms.LayoutEventHandler(this.Form2_Layout);}
#endregionprivate void Form2_Load(object sender, System.EventArgs e)
{
//你在这里加入验证;决定CheckClosed
this.CheckClosed=true;
}private void Form2_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
{
if(this.CheckClosed)
this.Close();
}
}
}
{
int i = 0;
if (i == 0) Application.Exit();
}
不过这样,可能有界面一闪的情况
Form里的私有数据,可以给出访问的属性,在调用方直接读取或设置其属性。
public void ShowMe()
{
if(条件)
this.Close();
else
this.Show();
}用form1.ShowMe();显示窗体