开发者

stop executing code after new form is loaded

开发者 https://www.devze.com 2023-04-12 23:02 出处:网络
Some 开发者_StackOverflowmethod call this method which has this code: Form frm = new Form(); frm.Show();

Some 开发者_StackOverflowmethod call this method which has this code:

Form frm = new Form();
frm.Show();

but i do not want to execute first method anymore after form is loaded. How can i prevent and stop loading code in forst form


Very unclear, I'm guessing that you want to make sure that only one instance of a form can be created. You do so by keeping track of the life of the instance. Like this:

    private Form2 instance;

    private void showForm2() {
        if (instance == null) {
            instance = new Form2();
            instance.FormClosed += delegate { instance = null; };
            instance.Show();
        }
        else {
            instance.WindowState = FormWindowState.Normal;
            instance.Focus();
        }
    }


Edit: question is very unclear so I give an answer based on my understanding of it...

to block execution after a form has been created, until such form is closed, try to use ShowDialog()

using(var frm = new Form1())
{
  frm.ShowDialog();

  // here your code is not executed until frm is closed...
  //...
  //...
}

Please pay attention that you do not want to create an object of type Form as that is the default base class and will not contain your controls...

0

精彩评论

暂无评论...
验证码 换一张
取 消