In an application that I have written there is a dashboard on the main form that is composed of a large docked scrollable panel that contains various other panels each showing various pieces of information. I have the AutoScroll property of the panel set to True so that if there are more modules/panels on the dashboard than can be visible on the screen, the user can scroll down to view them. When the user navigates to another window or another application, then returns to my application, and clicks one of the smaller panels on the dashboard, the large docked panel’s scroll position jumps up to the top. This was quite annoying as every time the user returned to my application they had to scroll back down to the module they were looking at previously.
Many of the solutions I found suggested manually setting the Panel.AutoScrollPosition when the form regains focus. The problem with this is that I would have had to capture the Click or MouseDown events of every single control on the form. I figured there had to be a better way to do this so I kept searching. I finally found a post on a forum that indicated that the issue was caused by the fact that the Panel.ScrollToControl method is called when my application regains focus. The ScrollToControl was scrolling to what the Panel control deemed to be the “activeControl” and thus caused the panel to jump to the top. So, to solve my problem, all I did was create a class that extended the Panel class and overrode the ScrollToControl method so that it returned the point associated with the currently visible portion of the panel.
public class CustomPanel : System.Windows.Forms.Panel
{
protected override System.Drawing.Point ScrollToControl(System.Windows.Forms.Control activeControl)
{
// Returning the current location prevents the panel from
// scrolling to the active control when the panel loses and regains focus
return this.DisplayRectangle.Location;
}
}
I presume that this might have side effects elsewhere but I haven’t found any so far in my situation.

June 12, 2010 at 8:59 pm
Tank you for this vvery usefull Workaround
June 22, 2010 at 2:16 pm
Glad it helped!
June 22, 2010 at 12:08 pm
Thanks, realy helped me with this!
August 13, 2010 at 6:45 am
Thank You very much! Works like a charm!
August 13, 2010 at 10:42 am
No problem!
September 7, 2010 at 6:12 am
Thank you for this neat solution!
It works for me!
September 7, 2010 at 7:06 am
I’m glad you found it useful!
December 5, 2010 at 4:43 am
Thanks a lot !!!!
February 3, 2011 at 2:07 pm
This tip just saved my sanity!!!!
I love you man and I love the InterWebs!
February 3, 2011 at 2:08 pm
Glad it helped! I about went crazy over this as well!
April 1, 2011 at 2:20 pm
Thanks for sharing.
A problem I encountered while using this is that adding controls to the panel (or increasing the size of the controls), will not cause the panel to autoscroll.
For exemple, if you have textbox in the panel and the textbox’ height increases, the panel will not autoscroll and the user will have to “scroll down” manually.
April 1, 2011 at 10:11 pm
Amazing, it works! Thanks man!!
April 10, 2011 at 10:43 pm
Thank you very much~
You saved my life~
April 12, 2011 at 9:25 am
Hi Nick, you just saved my life, I’ve been on this darn thing for more than a f***kin month!! Thank you so much, God Bless
August 1, 2011 at 2:26 pm
Thanks Nick! This has fixed a long-standing problem.
August 1, 2011 at 2:27 pm
No problem! I know how that feels. Glad it helped.
August 15, 2011 at 1:38 am
Exactly what i’m looking for ,
Thank you very very much
September 2, 2011 at 2:31 pm
Excellent! This is the perfect fix! I was going crazy capturing all the clicks and setting that darn AutoScrollPosition!
November 7, 2011 at 4:46 am
How to implement this in C# with ASP.NET?
November 7, 2011 at 7:41 am
Copy and paste the code above into a class file that resides in its own dll or in you project. Then you can do one of two things. You can then add a standard Panel to your form and then edit the designer class of that form and change the declaration statement of that panel from a System.Windows.Forms.Panel object to a CustomPanel object. Or, you can right click the Toolbar and add a custom control that references the dll or the project in which you placed the CustomPanel class. That will create a new icon for the CustomPanel control in the Toolbox and you can simply drag and drop that onto your form.
November 8, 2011 at 10:56 pm
SplitContainer has the same problem. And I try to resolve in your way. But it does not work.
February 1, 2012 at 1:57 pm
Hey Nick, thank you very much. If was dumbfounded by such a stupid problem.
February 8, 2012 at 10:00 pm
Simple and elegant solution!
Thank you very much!
March 9, 2012 at 4:14 am
This was one of the most useful tips I have ever got, thanks a million.
-R
June 26, 2012 at 1:30 pm
Wow, this is great! This absolutely made my day. I have a problem in a piece of software that has been bugging me and hindering my work for the past two years. This was the solution.
Weight off my shoulders. Thanks a million.
September 26, 2012 at 1:15 pm
Total win. thanks, solved my problem!
December 5, 2012 at 6:38 pm
Thanks a lot for this simple and elegant solution!
January 12, 2013 at 5:35 am
Thanks a lot for this simple and wonderfull solution
March 21, 2013 at 3:49 pm
Thank you! I beat my head against a wall for a while trying to fix this.
May 2, 2013 at 8:16 am
Hi Nick, thank for this post. It saved a lot of time. I was struggling to find out a viable solution for this behavior of System.Windows.Form.Panel control.
Thanks a ton.
Subha