WinForms/C# - PictureBox.Visible is ignored; resetting on VisibleChanged causes a stack overflow

Stern, Jason 96 Reputation points
2021-06-19T02:19:36.177+00:00

So this is a new one for me. I have a WinForm with two PictureBox instances. Their default value for Visible is true.

When the auto-generated "this.Controls.Add(this.___PictureBox);" gets invoked, Visible gets set to false.

If I try to set it back to true, [External Code] sets it back to false.

If I add a VisibleChange event handler to set Visible back to true, it leads to a stack overflow with only external code being executed.

Looking through PictureBox's documentation, I can't see anything that would force the PictureBox to not be visible. No exceptions are thrown, and given that Visible is a property, there is no error code returned.

Does anybody happen to know what I am missing?

107160-wat.png

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Stern, Jason 96 Reputation points
    2021-06-19T03:22:34.543+00:00

    With hours of life wasted - it appears that the problem is this:

    The Form I was instantiating was not initially displayed. It waited for a Windows event from an external process to then display. Because of this, no Form was ever visible at instantiation.

    If instead of creating the Form at start-up and defer displaying it, I create the Form and display it off-screen at instantiation, everything magically works despite no other logic changes.

    This feels like a bug in the .NET Framework somewhere. Best guess is a simple Control.Visible value getting cached somewhere that does not get updated when Control.Visible does, or something similar in Control.Load/Control.Show/etc. Or at a minimum, documentation missing from the MSDN.

    I know if I provide Microsoft with a reproducible test case for this, they will say "you need enterprise level support to get us to look into this". And I know if I get my company to open a ticket about this, they will charge us thousands to come back with the response of "Have you seen this thread? This is how someone else worked around it" instead of actually fixing the underlying issue.

    So hopefully I saved someone hours of their life should they encounter this.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2021-06-19T03:26:02.967+00:00

    Unsubscribe from the event e.g.

    public Form1()
    {
        InitializeComponent();
        pictureBox1.VisibleChanged += PictureBox1OnVisibleChanged;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
    }
    private void PictureBox1OnVisibleChanged(object? sender, EventArgs e)
    {
        pictureBox1.VisibleChanged -= PictureBox1OnVisibleChanged;
        pictureBox1.Visible = true;
        pictureBox1.VisibleChanged += PictureBox1OnVisibleChanged;
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.