OnVisibleChanged quagmire
I have a control that is becoming nonvisible and I cant seem to find the source. What I thought would be an easy problem to solve by hooking the OnVisibleChanged event, turns out to be a confusing and difficult problem. Hooking the controls OnVisibleChanged event did not allow me to determine when the control was being hidden. In fact, the event never fired (and the parent is visible).
I understand a bit of the background that child controls do not receive this event if their parent changes visibility and that the Visible property will return False if any parent in the parent chain is not visible, but I evidently don't understand when and why this event is triggered or how to make use of it. I would have thought that the event would have been triggered in order to make my contol hide.
I see that the event is triggered through a direct setting of the Visible property and that works fine. I don't understand why it is called when a control is reparented. In fact, it is called several times only to return the control to it's original state. Why is this so? Why is it that changing a parent's visibility does NOT trigger the event, but changing the parent DOES?
That would be a good start to understanding this event, but since it does not fire when parent visibility is changed, it is not going to help me in the long run. I have a control that polls for data and I want to be able to turn the polling off whenever the control is hidden (and not turn it off during reparenting). Is there some other event or method I'm missing that will enable me to determine when my control actually becomes hidden/visible?
Thanks,
Ray
That will solve half of the problem. That is, it sounds like I'll be able to determine where my control was hidden, but I'm curious how this would be different than listening to the VisibilityChanged event and why the VisibilityChanged event wouldn't be triggered?
The other half of the problem is for the child control to know when it has become hidden. The problem here is that the VisibleChanged event is not fired when a control's parent is hidden.
As far as I can tell, Visibility has three states:
1) Not visible
2) Visible (and all parents are visible)
3) Visible but not shown (the parent chain isn't completely visible).
Controls should know when they change state but Visible { get; } does not distiquish between 1 & 3 and VisibleChanged is not raised in all state changes.
In case 3, Visible will return false so even if I did receive the VisibleChanged event and walked the parents, I would not be able to distiguish between state 1 & 3 because Visible would return false in case 3.
- Ray
There is a 4th state:
4) Visible == true but not physically visible.
when a control is created, Visible == true but the control is not pysically visible. Visible will continue to be true until the control is parented to a parent chain where one of the parents is hidden or control.Visible is set to false (or perhaps Hide()). This at first glance appears to be the same as 2) except in #2, I was talking about the parents being phyically visible (and therefore the child is pysically visible). In this case the control isn't pysically visible.
So, if this property is supposed to be "a value indicating whether the control is displayed." it would appear that it is only dependable when False as far as physical display (modulo zorder, etc) and not dependable when it's state is True.
What does this property really represent?
- Ray