This tip is compliment to the tip at:
Extension method to make a control visible in complex hierarchy[
^]
Consider the scenario where you have multiple nested tabcontrols:
TabControlMain
TabPageMainOne
TabControlSubOne
TabPageSubOne
TabPageSubTwo
MyControl
TabPageMainTwo
TabControlMoreSubs
TabPageMoreSubsOne
TabPageMoreSubsTwo
The common (naive) approach to determining whether
MyControl
is currently visible on screen is either cumbersome or error prone:
bool isVisible = TabControlSubOne.ActivePage == TabPageSubOne
&& TabControlMain.ActivePage == TabPageMainOne;
This kind of works, until the screen is turned into a
usercontrol
and placed on a third
TabControl
. Now there is another control to consider, but your code was not designed to do so. This is a common scenario in dynamically composed screens.
Another problem with this method occurs within the screen itself and has to do with maintainability. What happens if we redesign the screen and decide that
MyControl
should be moved to
TabPageMoreSubsTwo
? Now our test is invalid, but because the code itself is not obviously wrong, we might not discover the problem until it is too late (production).
So obviously, this is less than desirable. What went wrong?
Let's return to the original problem and the way it is implemented. The question asked was 'Is My Control Visible' but it was implemented as: 'Have some tabcontrols the correct active page property'. The problem with the implementation is the word 'some' as I have shown above that checking 'some' might not be enough, or even be the wrong ones.
To solve the problem, we should implement this as 'Have ALL tabcontrols the correct active page property'? which is what this extension method does.
Usage:
bool isVisible = MyControl.IsMyControlVisible();
This extension method has a nice benefit that we abstract away the 'how' and only the 'what' of the source code remains. +1 For readability.
public static bool IsMyControlVisible(this Control subject)
{
if ((subject == null) || (subject.Parent == null))
{
return false;
}
Control testSubject = subject;
TabControl parentPageControl = null;
UltraTabPageControl aPage = null;
Control testSubject = subject;
while (testSubject != null)
{
TabPage asTabPage = (testSubject as TabPage);
if (asTabPage == null)
{
UltraTabPageControl asUltraTabPage = testSubject as UltraTabPageControl;
if (asUltraTabPage != null)
{
if (asUltraTabPage.TabControl == null)
{
return false;
}
if (asUltraTabPage.TabControl.SelectedTab != asUltraTabPage.Tab)
{
return false;
}
}
}
else
{
TabControl parentPageControl = asTabPage.Parent as TabControl;
if (parentPageControl == null)
{
return false;
}
if (parentPageControl.SelectedTab != testSubject)
{
return false;
}
testSubject = parentPageControl;
}
if (testSubject != null)
{
testSubject = testSubject.Parent;
}
}
return true;
}