Tuesday, November 16, 2010

ScrollToControl (.NET C#)

If you are using a Panel you may notice that it jumps around when you restore focus. It is actually jumping to the xy position of the current control within the Panel. This is due to implementation calling ScrollToControl. For example I have an application that uses a Panel as a convenient alternative to creating a ScrollableControl. My control is the only one in the Panel and I wanted to take advantage of the background image along with the auto-scroll functionality. My Panel is a specialized derivative of Panel so I went ahead and created the override below:

   1:  protected override Point ScrollToControl(Control activeControl)
   2:  {
   3:      if (m_bDisableScrollToControl)
   4:      {
   5:      return AutoScrollPosition;
   6:      }
   7:      return base.ScrollToControl(activeControl);
   8:  }

Basically I only allow the ScrollToControl if my custom boolean is enabled. In the case of a single control it will likely never be enabled. Instead I just leave the position as-is based on the AutoScrollPosition.

1 comment: