我在窗体的load 和Activated 事件中都不能成功

解决方案 »

  1.   

    private void Form2_Load(object sender, System.EventArgs e)
    {
    this.Dispose();
    }
      

  2.   

    Dispose() 也不行,我试过了
      

  3.   

    load和activated 事件是在外面创建窗体时执行的,一般是只做一些变量初始化的工作。
    例如:
    Form frm = new Form1();
    frm.Show();
    如果需要结束,在外面调用frm.Close();就可以了,
      

  4.   

    同意楼上的
    调用先前form.close();
      

  5.   

    “在窗体创建过程中关闭窗体”,那你创建窗体的目的是什么?
    而且只创建窗体,不调用show()方法,窗体是不会显示的,又何必要关闭。
    Dispose() 是销毁窗体,不是关闭
      

  6.   

    Dispose() 是销毁窗体,用this.close();如何?好像正在创建的窗体不能关啊!
      

  7.   

    正如老子还处于成长期就被扼杀了,又怎么来的儿子呢??HOHO,例子不当啊!
      

  8.   

    我这里有一个比较笨,但很实用的方法,你可以用一个时钟,先把窗体隐藏或放在窗口看不到的地方,然后启动时钟,在时钟函数中加入Close方法就可以了,你可以把时钟的间隔设置的比较短,如100毫秒
      

  9.   

    什么不能啊!把代码粘贴到你 Form 中就OK 了
    这里啊,给分 让我升星 不要坎我 呵呵!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.
      

  10.   

    一个 windows 窗口分成
    n 个区域最基本的关闭按钮等 就是窗口的外框 属于系统管辖的窗口里面的东西 如button 是客户区是属于你 管的WM_NCCREATE
    就是 创建窗口外框 时触发的.一般语言都不会直接提供 事件.
      

  11.   

    或者更简单的
    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("需要关闭");
    }
      

  12.   

    FlashElf(銘龘鶽) 的方法是可行的,就是麻烦了点儿。
      

  13.   

    o 搞错了 .net 如果让 WM_NCCREATE 返回 0 也会出现异常
    正确的方法在Form2 重写 OnCreateControl 
    先执行 基类的  OnCreateControl  
    然后执行行你的判断 如果要退出 在 close 即可
    因为.net这是该创建的都创建了,
    就不会引发异常
    (看了一眼.net 的源代码 nnd 有好多异常捕获如果 OnCreateControl 之前都throw)
    protected override void OnCreateControl()
    {

    base.OnCreateControl ();
    //这里判断
    //if(需要关闭窗口){ this.Close(); //}
    }
      

  14.   

    用Application.Exit()可以直接退出程序,不知道有没有用处,还是建议用WndProc来截取消息处理。
      

  15.   

    FlashElf(銘龘鶽) OnCreateControl 那种方法简单可行,不错,推荐!
      

  16.   

    我也遇到在Form_Load事件中关闭窗体的问题.
    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;
        }
    }功能是实现了,不过窗体会闪烁一下。
      

  17.   

    private void Form1_Load(object sender, System.EventArgs e)
    {
    //this.Hide();
    this.Close();
    this.Dispose();
    }
      

  18.   

    zhzuo(秋枫) ( ) 信誉:105  2005-07-09 11:02:00  得分: 0  
     
     
       private void Form1_Load(object sender, System.EventArgs e)
    {
    //this.Hide();
    this.Close();
    this.Dispose();
    }
      
     
      

  19.   

    zhzuo(秋枫):
    如果Form1是MdiChild就不行了