Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

C# How do I handle a one time startup condition for a form if the form is generated new each time?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...