this.Visible is not working in Windows Forms

Yes, the Visible property is a big deal in Windows Forms, that’s what actually gets the handle created and causes OnLoad() to run. In other words, the window doesn’t exist until it gets visible. And it will ignore attempts to undo this.

It is pretty common to want to still create the handle but not make the window visible if you use a NotifyIcon. You can achieve this by overriding SetVisibleCore:

protected override void SetVisibleCore(bool value) {
    if (!this.IsHandleCreated) {
        value = false;
        CreateHandle();
    }
    base.SetVisibleCore(value);
}

Beware that OnLoad still won’t run until the window actually gets visible so move code into the constructor if necessary. Just call Show() in the NotifyIcon’s context menu event handler to make the window visible.

Leave a Comment