Hidden parent, visible child, VisibleChanged
I have a container that is to be displayed only when child controls are visible. I hook the VisibleChanged event to detect the child controls transition from visible to not visible to hide the container but if the container is invisible, I cannot detect that the child has become visible.
Is there a way to detect a child has become visible from the parent when the parent is not visible? The VisibleChanged event does not provide an argument (I was hoping for the 'internal' visibility) and child.Visible will always return false when the parent is not visible.
Thanks,
Ray
I just found an interesting side effect of this problem that may be
an answer, but at the cost of performance. A control without a parent will report it's correct visibility. I suppose it would possible to remove the control from the collection while it is hidden and when it becomes visible, reparent it. Any other solutions out there?
Thanks,
Ray
This is by design. If a container control is not visible, it's children controls will not be visible either. In my opinion, this is a good thing.
When you set your child control to be visible, you could walk it's parent hierarchy and set them to be visible as well. You could move your containers to be off-screen, say at {-1000, -1000} (an old trick from VB3 days). Or, as you say, you could remove the container from the form and cache it somewhere else. Still, you would have to wrap your childcontainer.visible = true/false call in a method to contain this additional logic.
Another idea is to hook the message setting the control visible to true, and send a visible = true message to the parents. I haven't tried this, maybe it won't help.
Yes, I understand this behavior is by design and there is nothing wrong with it. The problem is that you cannot detect when a child window wants to be visible.
Walking the hierarchy is exactly what I wanted to do -- automatically using VisibleChanged hooks. The problem is that the VisibleChanged event does not provide the required state. Perhaps making use of the EventArgs to pass in the 'true' state of the child would be a good addition to the design.
You should also note that Web.UI.Controls.Visible is virtual whereas Windows.Forms.Controls.Visible is not. This means I can't even add the state information to the event in an OnVisibleChanged override.
The not so ideal solution is to make a call in the event handler to Win32 to get the VS_VISIBLE bit. This will be set even if child.Visible is false and parent.Visible is false. I'd rather not be calling unmanaged code directly, but it would appear to be the only option at this point.
- Ray
SetVisibleCore is interesting and related, but it does not address the original question:
How does a parent control detect that a child control has become visible when the parent isn't visible?
- Ray
While I don't know this for a fact, and I am trying to determine this, my expectation is the parent doesn't know. I would expect the child is responsible for knowing whether it is visible or not (considering it's parents), and so any logic based upon the child's visibility would be pushed from the child, e.g., by overriding SetVisibleCore.
I'll post what I learn.