您的位置:程序门 -> .net技术 -> c#



在form_load事件中进行某些判断,通不过则不加载窗体。能否做到?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


在form_load事件中进行某些判断,通不过则不加载窗体。能否做到?[已结贴,结贴人:ymli]
发表于:2007-03-23 10:59:56 楼主
请教:要求在form_load事件中进行某些判断,通不过则不加载窗体。请问能否做到?(由于某些条件限制,不能在窗体frmorder.show()之前进行判断)  
代码:        
                private   void   frmorder_load(object   sender,   eventargs   e)
                {
                        this.cursor   =   cursors.waitcursor;

                        try
                        {
                                ……              
                        }
                        catch   (exception   ex)
                        {
                                messagebox.show(ex.message);
                                this.close();   //这句不可行,出错。怎么改?
                        }
                        finally
                        {
                                this.cursor   =   cursors.default;
                        }
                }
发表于:2007-03-23 11:05:521楼 得分:0
catch   (exception   ex)
{
messagebox.show(ex.message);

}
finally
{
this.cursor   =   cursors.default;
this.close();   //放到这里
}
}
发表于:2007-03-23 11:16:562楼 得分:0
同样出错
而且,这样岂不是无论怎样都会关闭窗体了么?
发表于:2007-03-23 11:19:343楼 得分:10
对于   mainform   的加载控制
最好在   program.cs   里   new   mainform   之前做判断
发表于:2007-03-23 11:25:244楼 得分:0
finally里不就直接喀嚓了么
发表于:2007-03-23 11:25:375楼 得分:0
放到构造函数里
发表于:2007-03-23 11:29:456楼 得分:10
自己写一个public方法,比如showme

在里面判断,不过则不加载窗体

调用frmorder.showme()
   
*****************************************************************************
欢迎使用csdn论坛专用阅读器   :   csdn   reader(附全部源代码)  

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
发表于:2007-03-23 11:53:527楼 得分:10
你不能在load事件里关闭本窗体!

你最好把这个操作放到构造对象之前,就是如果条件不成立就不new这个窗体.

你可以把条件拿出来做为公用的部分来判断,而不要放到窗体的load事件里.
发表于:2007-03-23 11:56:568楼 得分:0
catch   (exception   ex)
{
messagebox.show(ex.message);
this.close();//呵,傻了~因为你先关闭了
}
finally
{
this.cursor   =   cursors.default;//到这里就继续出错
去掉
}
}
发表于:2007-03-23 12:00:539楼 得分:5
最好自己写一个重写show方法,测试通过就base.show,否则就...
发表于:2007-03-23 12:09:1410楼 得分:0
还不明白?
this.cursor   =   cursors.waitcursor;

try
{
……
}
catch   (exception   ex)
{
messagebox.show(ex.message);
this.close();   //这句不可行,出错。怎么改?
}
this.cursor   =   cursors.default;

你应该这样
发表于:2007-03-23 12:29:3411楼 得分:5
load事件中不能够去写关闭代码很多winform类的语言都是这样的。
还是应该将判断写在show方法之前。
发表于:2007-03-23 12:33:0012楼 得分:10
最好的方法是将判断放到构造对象之前...

如果你没有更改,默认应该是在program.cs中..

类似这样..
static   class   program
        {
                ///   <summary>
                ///   应用程序的主入口点。
                ///   </summary>
                [stathread]
                static   void   main()
                {
                        application.enablevisualstyles();
                        application.setcompatibletextrenderingdefault(false);
                        if   (满足条件)
                        {
                                  application.run(new   form1());
                          }
                }
        }
发表于:2007-03-23 12:37:4213楼 得分:0

当你catch到错误之后,就把this关了,this已经不存在了,
然后finally那里又调用了this,所以就出错咯
发表于:2007-03-23 12:41:0414楼 得分:0
我试过了,你那样写也没问题,没有出错,我在vs2003下面,也没有问题啊,只是窗口闪了一下就消失了;
private   void   form1_load(object   sender,   system.eventargs   e)
{

try
{
system.convert.toint32( "ds ");
  }
catch   (exception   ex)
{
messagebox.show(ex.message);
this.close();   //这句不可行,出错。怎么改?
}
finally
{
this.cursor   =   cursors.default;
}


}
发表于:2007-03-23 13:15:2115楼 得分:0
帮顶   !


快速检索

最新资讯
热门点击