请教:要求在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;
}
}
代码:
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;
}
}
{
MessageBox.Show(ex.Message);}
finally
{
this.Cursor = Cursors.Default;
this.Close(); //放到这里
}
}
最好在 Program.cs 里 new MainForm 之前做判断
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
{
MessageBox.Show(ex.Message);
this.Close();//呵,傻了~因为你先关闭了
}
finally
{
this.Cursor = Cursors.Default;//到这里就继续出错
去掉
}
}
this.Cursor = Cursors.WaitCursor;try
{
……
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.Close(); //这句不可行,出错。怎么改?
}
this.Cursor = Cursors.Default;你应该这样
还是应该将判断写在show方法之前。
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (满足条件)
{
Application.Run(new Form1());
}
}
}
当你catch到错误之后,就把this关了,this已经不存在了,
然后finally那里又调用了this,所以就出错咯
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;
}
}