C# 关于在winforms中的窗体构造函数中关闭窗体的问题
我的要求是,如果表单不满足某些条件,我需要在显示表单之前关闭表单。为此,我编写了Showed事件,但它在代码中显示的某个位置工作 我写的代码是C# 关于在winforms中的窗体构造函数中关闭窗体的问题,c#,winforms,C#,Winforms,我的要求是,如果表单不满足某些条件,我需要在显示表单之前关闭表单。为此,我编写了Showed事件,但它在代码中显示的某个位置工作 我写的代码是 public form1() { Initialize component(); if(some condition) { if(some condition) { if(some condition) { }
public form1()
{
Initialize component();
if(some condition)
{
if(some condition)
{
if(some condition)
{
}
else
{
this.Shown += new EventHandler(MyForm_CloseOnStart);//here form is displaying event is not working
}
}
else
{
this.Shown += new EventHandler(MyForm_CloseOnStart);//here only form is closing before displaying the form
}
}
else
{
if(some condition)
{}
else
{this.Shown += new EventHandler(MyForm_CloseOnStart);}//here form is displaying shown event is not working
}
}
public void MyForm_CloseOnStart(object sender, EventArgs e)
{
this.Close();
}
假设我想根据flag变量的值关闭表单。然后:
public Form1(Boolean flag)
{
try
{
if (flag)
InitializeComponent();
else
Environment.Exit(0);
}
catch (Exception)
{
}
}
你的问题令人费解。请重新措辞。@Yamuna Pattem把你的问题说清楚。只有这样我们才能回答。@yamuna 1。)你的问题本身不清楚,你真的应该重新措辞,最好包括一张你想要采取的行动的图表。2.)显示的代码是…..让我们称之为有趣的是,您连续检查3次相同的条件是否为真,其间没有任何可能的更改,除此之外,还使用else路径中的smae条件…….我真诚希望该条件仅用于占位符,否则我建议阅读taht语句在代码方面完全不合逻辑。在Load事件运行之前,无法关闭窗体。可以考虑在构造函数中抛出异常。然后,客户机代码知道调用Show()是不可取的。或者将Show()调用移动到Form类的公共方法中。或者将条件代码移动到一个单独的静态方法中,以便客户机知道根本不创建表单对象。有很多选择,你必须选择你最喜欢的。相反,我也给出了加载事件,但这只在一个地方运行良好