Search code examples
c#winformsdebuggingvisibilitystep-into

What unusual thing takes place when I step into a Control.Visible = true assignment?


I have an UserControl that is made Visible. One of its custom properties is set to true somewhere I do not know. All I know is that after setting its Visible property to another value (true), the custom property is true. It is wrong that its value is set to true, it should remain false like it was before.

I am using .NET Framework 4.6.1. I have searched on StackOverflow and I did not find something useful. I tried using the debugger and the Watches window, this is how I found the information presented.

I debugged the program, and the value changes precisely when I step into the Visible = true attribute, before other code is shown. The other code is the OnPaint handler of my UserControl, and there, in the first line of code, the custom property already has this wrong value. There is no way to find what happens internally after stepping into the Visible assignment and before the custom property takes the value true. I guess it is something like an Application.DoEvents() call or another thread. How can I debug this so that I get to the code that changes the value of my custom property?

I have read the official documentation and I did not find something useful.

internal void SetChildVisible(ClockData td, bool v)
{
    foreach (IClockView tv in td.MyTimerViews)
    {
        if (tv is ClockControl tc &&
            tv.GetClocksView() == MyClockListView)
        {
            tc.Visible = v;
            break;
        }
    }
    MyClockListView.RefreshDisplay();
}

I would like to have some option somewhere in Visual Studio so I can debug situations like this.


Solution

  • I found the problem by putting a breakpoint in the custom property's setter. The debugger steps into the setter before the Visible property of the Control starts being set. In the setter the property was already set to false, so no change was executed, so I stepped out to see where the property was being changed from true to false the last time.

    The custom property is a redirect to a subcontrol's custom property for which the code was the following:

    internal bool ShowCheckBox
    {
        get
        {
            return cb.Visible;
        }
        set
        {
            if (cb.Visible != value)
            {
                cb.Visible = value;
                OnShowCheckBoxChanged();
            }
        }
    }
    

    The problem was that I was setting the cb.Visible property and, before that thread which makes it in/visible finished, I was setting the ShowCheckBox property again and cb.Visible was still having the old value. The solution was to change the above code into the following:

    internal bool _ShowCheckBox = true;
    internal bool ShowCheckBox
    {
        get
        {
            return _ShowCheckBox;
        }
        set
        {
            if (_ShowCheckBox != value)
            {
                _ShowCheckBox = value;
                OnShowCheckBoxChanged();
            }
        }
    }