private void FocusChanged()
{
bool focusedControlIsWithinMe = VisitControlTree((control) =>
{
return(control == Application.FocusManager.FocusedControl);
});
if (focusedControlIsWithinMe)
{
var offset = Application.FocusManager.FocusedControl.CalculateRelativePosition(this);
var visibleWindowBounds = new Rectangle(HorizontalScrollUnits, VerticalScrollUnits, Width, Height);
var focusedControlBounds = new Rectangle(offset, Application.FocusManager.FocusedControl.Size);
if (focusedControlBounds.IsAbove(visibleWindowBounds))
{
int amount = visibleWindowBounds.Top - focusedControlBounds.Top;
VerticalScrollUnits -= amount;
}
if (focusedControlBounds.IsBelow(visibleWindowBounds))
{
int amount = focusedControlBounds.Bottom - visibleWindowBounds.Bottom;
VerticalScrollUnits += amount;
}
if (focusedControlBounds.IsLeftOf(visibleWindowBounds))
{
int amount = visibleWindowBounds.Left - focusedControlBounds.Left;
HorizontalScrollUnits -= amount;
}
if (focusedControlBounds.IsRightOf(visibleWindowBounds))
{
int amount = focusedControlBounds.Right - visibleWindowBounds.Right;
HorizontalScrollUnits += amount;
}
}
}