C#如果每次都生成新表单,我如何处理表单的一次启动条件? [英] C# How do I handle a one time startup condition for a form if the form is generated new each time?
问题描述
我有形式的A
,它在一个事件后打开形式B
然后隐藏自己。 表格B
为表格A
生成条件,然后返回到表格A
>并关闭;但是,由于与代码冲突,我必须重新生成 form A
,以免遇到一个stackoverflow异常/我的应用程序无法正常关闭(由于形式A
,主窗体被隐藏)
这有点让人困惑,基本上我已经通过声明main然而,我希望能够处理首次启动事件,例如某些应用程序的教程消息。没有必须创建日志来存储我的布尔值的开销,我将如何检测它是否是第一次打开表单?
通常我会:
Event(){
bool startup;
if(startup = true){
startup = false;
return;
}
//其余代码
}
然而,因为每次都会产生新的表单,所以这个表单将始终保持为真。
以下是代码:
表单一个变量:
Account AccountForm = new Account();
按钮事件:
AccountForm.QR = this;
this.Hide();
AccountForm.ShowDialog();
表格B:
public Form QR {get;组; }
按钮事件:
QR = new QueryRefiner();
this.QR.Show();
this.Close();
我认为这就是全部。我从QR中取出新的声明,并且会收到StackOverflow异常。我想我应该为此创造一个新的问题,但它就是这样。 (我认为我还在计时器中)
好了,一个简单的解决方案是在 FormA
来获取一个参数,指示它是从 FormB
创建的,如下所示:
public FormA(bool byFormB)
{
if(byFormB)
{
//做你必须做的从FormB
}
}
创建它,然后创建它从 FormB
: FormA frm = new FormA(true);
无论如何,我不会每次创建新的 FormA
,只是隐藏/显示它。
I have form A
, which opens form B
after an event then hides itself. Form B
generates conditions for form A
then returns to form A
and closes itself; however, due to conflict with code, I will have to generate form A
anew lest I encounter a stackoverflow exception / my application not closing properly (due to form A
, the main form, being hidden)
This has gotten a bit confusing, essentially I've already solved this by declaring the main form as new each time, however, I wish to be able to handle first time startup events like those tutorial messages for certain applications. Without the overhead of having to create a log to store my boolean, how will I detect if it's the first time the form is opened?
Normally I would:
Event(){
bool startup;
if (startup = true) {
startup = false;
return;
}
//Rest of code
}
However, since the form is generated new each time, this will always remain true.
Here's the code:
Form A variables:
Account AccountForm = new Account();
Button event:
AccountForm.QR = this;
this.Hide();
AccountForm.ShowDialog();
Form B:
public Form QR { get; set; }
Button event:
QR = new QueryRefiner();
this.QR.Show();
this.Close();
This is all of it I think. I take the new declaration out of QR, and I would receive a StackOverflow exception. I guess I should have created a new question for this, but there it is. (I think i'm on the timer still)
Well,an easy solution is to make a new constructor in your FormA
to get a parameter indicating that it is created from FormB
, something like:
public FormA(bool byFormB)
{
if (byFormB)
{
//do what you have to do when it's created from FormB
}
}
And just create it like this from FormB
: FormA frm=new FormA(true);
Anyway, I would not create a new FormA
each time, just hide/show it.
这篇关于C#如果每次都生成新表单,我如何处理表单的一次启动条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!