我在窗体的load 和Activated 事件中都不能成功
解决方案 »
- 在C#中如何最快的加载图片到控件上
- 求C# WINMobile 下实现FTP的上传下载!!!
- C#往oracle数据库插入记录无反应
- 如何从官网获取开奖结果
- Form load 时 ,用Graphics的方法画不了图?
- 可能是BUB 不完美的 oledbdatadapter.ExecuteNonQuery() 问题 提示 由于将在索引、 主关键字、或关系中创建重复的值 请高手解决.
- c#2005中如何改变listview指定行和指定列和指定单元格的颜色?
- c#除了使用microsoft pivottable做数据透视表还可以用其它控件吗?
- 如何初始化struct结构中的byte数组?
- 請問有沒用我得程式怎麼說using System.WinForms不存在而編譯不了阿
- 语法问题
- 如何在c#中模拟鼠标单击事件。
{
this.Dispose();
}
例如:
Form frm = new Form1();
frm.Show();
如果需要结束,在外面调用frm.Close();就可以了,
调用先前form.close();
而且只创建窗体,不调用show()方法,窗体是不会显示的,又何必要关闭。
Dispose() 是销毁窗体,不是关闭
这里啊,给分 让我升星 不要坎我 呵呵!protected override void WndProc(ref Message m)
{
const int WM_NCCREATE = 0x0081;
if (m.Msg == WM_NCCREATE)
{
//在这里写你的处理
if (需要关闭窗口)
{
m.Result=IntPtr.Zero; //如果 m.Result = IntPtr.Zero; 就是不创建窗口
}
}
base.WndProc (ref m);
}
下面是 ms 的说明 我英文也不好 不过基本能看懂了以前也用过
C# 代码就是上面那个样的WM_NCCREATE Notification--------------------------------------------------------------------------------The WM_NCCREATE message is sent prior to the WM_CREATE message when a window is first created.A window receives this message through its WindowProc function.
SyntaxWM_NCCREATE WPARAM wParam
LPARAM lParam;
ParameterswParam
This parameter is not used.
lParam
Pointer to the CREATESTRUCT structure that contains information about the window being created. The members of CREATESTRUCT are identical to the parameters of the CreateWindowEx function.
Return ValueIf an application processes this message, it should return TRUE to continue creation of the window. If the application returns FALSE, the CreateWindow or CreateWindowEx function will return a NULL handle.
n 个区域最基本的关闭按钮等 就是窗口的外框 属于系统管辖的窗口里面的东西 如button 是客户区是属于你 管的WM_NCCREATE
就是 创建窗口外框 时触发的.一般语言都不会直接提供 事件.
private void button1_Click(object sender, System.EventArgs e)
{
Form2 f= new Form2();
try
{
f.Show();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
f.Dispose();
}
}private void Form2_Load(object sender, System.EventArgs e)
{
throw new Exception("需要关闭");
}
正确的方法在Form2 重写 OnCreateControl
先执行 基类的 OnCreateControl
然后执行行你的判断 如果要退出 在 close 即可
因为.net这是该创建的都创建了,
就不会引发异常
(看了一眼.net 的源代码 nnd 有好多异常捕获如果 OnCreateControl 之前都throw)
protected override void OnCreateControl()
{
base.OnCreateControl ();
//这里判断
//if(需要关闭窗口){ this.Close(); //}
}
FlashElf(銘龘鶽)的在WndProc()和OnCreateControl()中处理的方法我试了是不行的,不知大侠是如何实现的?其中WndProc()中处理后,依然会执行Form_Load事件。我用委托的异步调用方法基本解决了。
private delegate void CloseForm();
private void CloseInLoad()
{
this.Close();
}
private void frmBol_Load(object sender, System.EventArgs e)
{
//其它代码不写了,主要是读取数据处理了
if(this.cmbPayType.Items.Count == 0)
{
//提示基本资料没有设定
CloseForm close = new CloseForm(CloseInLoad);
close.BeginInvoke(null,null);//委托异步调用
return;
}
}功能是实现了,不过窗体会闪烁一下。
{
//this.Hide();
this.Close();
this.Dispose();
}
private void Form1_Load(object sender, System.EventArgs e)
{
//this.Hide();
this.Close();
this.Dispose();
}
如果Form1是MdiChild就不行了