-1

I have a TabControl in a Winforms app and on one of the tab pages, I have a label I use to inform the user of data deficiencies on the page. When the deficiencies are corrected, I hide the label, and make visible instead another label that indicates the data is valid.

The problem is, when the user switches to another tab, then comes back, both labels are visible. The problem seems to be that I need to rebuild various elements when the user selects the tab, and at the point I do this changing the visibility on the controls has no effect, which I am guessing is because the tabpage is not yet visible itself. I have tried to do it in the TabControl's Selected event, and in the tabpage's Enter event, but neither one has worked.

I have looked for an appropriate event - one occurring after the tabpage becomes visible, but before it is displayed to the user (or even immediately after it is displayed), but have not found one.

Is there an event that would be appropriate for setting visibility of controls on the tabpage? Or is there some other method of setting visibility before the tabpage is displayed?

Paul Sinclair
  • 223
  • 1
  • 12
  • 1
    Please post a simple code to reproduce the problem. – Reza Aghaei Mar 30 '16 at 19:42
  • @RezaAghaei - well, when I tested a simple code, it didn't do it. I guess I need to isolate the cause in my complex application. – Paul Sinclair Mar 30 '16 at 20:15
  • The first benefit of creating a simple code to reproduce the problem is for you. Usually it helps you to find problem yourself or moves you to right direction or at least keeps you away from wrong directions. After that if the problem exists in simple code then the code will be a good start point for other users to help you :) – Reza Aghaei Mar 30 '16 at 20:21

1 Answers1

0

The problem turned out to be that I was setting visibility on one of the labels by a function call then setting the other to be the negation of the first:

label1.Visible = IsValid();
label2.Visible = !label1.Visible;

But per this answer, label1.Visible doesn't return the visibility setting on label1 alone, but the lowest visibility of label1 and its parents. Since the tabpage was not visible, when IsValid() is true, label1.Visible is set to true internally but still returned false. So label2.Visible was set to true as well. Once the tabpage was shown, the visibilities reverted to their internal values and both labels were shown.

The solution was to store the value in a variable:

bool IsGood = IsValid();
label1.Visible = IsGood;
label2.Visible = !IsGood;
Community
  • 1
  • 1
Paul Sinclair
  • 223
  • 1
  • 12