Load a form without showing it

前端 未结 8 1086
梦谈多话
梦谈多话 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 20:31

    None of the answers solved the original question, so, add the below, call .Show() to load the form without showing it, then call .ShowForm() to allow it to be visible if you want to after:

    private volatile bool _formVisible;
    protected override void SetVisibleCore(bool value)
    {
        base.SetVisibleCore(_formVisible);
    }
    public void ShowForm()
    {
        _formVisible = true;
        if (InvokeRequired)
        {
            Invoke((Action) Show);
        }
        else
        {
            Show();
        }
    }
    

提交回复
热议问题