Load a form without showing it

前端 未结 8 1087
梦谈多话
梦谈多话 2020-12-09 19:39

Short version: I want to trigger the Form_Load() event without making the form visible. This doesn\'t work because Show() ignores the current value of the Visible property:<

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 20:29

    Perhaps it should be noted here that you can cause the form's window to be created without showing the form. I think there could be legitimate situations for wanting to do this.

    Anyway, good design or not, you can do that like this:

    MyForm f = new MyForm();
    IntPtr dummy = f.Handle; // forces the form Control to be created
    

    I don't think this will cause Form_Load() to be called, but you will be able to call f.Invoke() at this point (which is what I was trying to do when I stumbled upon this SO question).

提交回复
热议问题