Load a form without showing it

前端 未结 8 1069
梦谈多话
梦谈多话 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:37

    Move mandatory initialization code for the form class out of the Load event handler into the constructor. For a Form class, instantiation of an instance (via the constructor), form loading and form visibility are three different things, and don't need to happen at the same time (although they do obviously need to happen in that order).

    0 讨论(0)
  • 2020-12-09 20:42

    From MSDN:

    Form.Load
    Occurs before a form is displayed for the first time.

    Meaning the only thing that would cause the form to load, is when it is displayed.
    Form.Show(); and Form.Visible = true; are the exact same thing. Basically, behind the scenes, Show checks for various conditions, then sets Visible to true. So obviously, setting visible to false (which it already is) before showing the form is meaningless.

    But let's forget the technicalities. I completely agree with Rich B and Shaun Austin - the logic shouldn't be in that form anyway.

    0 讨论(0)
提交回复
热议问题