0

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.

silviubogan
  • 3,343
  • 3
  • 31
  • 57
  • Try to uncheck checkbox _Step over properties and operations_ in _Debugging_ -> _General_ options tab. – Miamy Mar 28 '19 at 14:43
  • Do you have the source code for the UserControl? – tgolisch Mar 28 '19 at 15:46
  • @tgolisch Yes, I do have the source code of the UserControl. – silviubogan Mar 28 '19 at 15:49
  • 1
    Have you tried putting a break-point on the getter and setter for the custom property and any underlying code? – tgolisch Mar 28 '19 at 16:04
  • Setting the Visible property of UC, causes a call to [CreateControl()](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,5827) (reference source) for the UC and the its child controls. See whether this can change anything in your custom properties. – Jimi Mar 28 '19 at 16:49

1 Answers1

0

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();
        }
    }
}
silviubogan
  • 3,343
  • 3
  • 31
  • 57