views:

232

answers:

1

Why does scrolling position change after refocusing the Form?

Situation:

Design mode and application just started Remark the scroll panel position.

Design and start

Scrolling manually the panel to left with mouse

alt text

Changing the focus to other application and refocusing back our application(click/click)

alt text

You can see that when the application loses the focus and then retrieves it back, the scroll position is "reseted".

Why and how stop it?

+2  A: 

It happens because when the form regains focus, it will focus on a control within the form (setting ActiveControl). Since it looks like your controls are to the right, it fill focus on them and move to the right. A quick fix would be to add a panel to the form and set the Dock to Fill and AutoScroll to True, then add all your controls on top of the panel. Then the form will focus on the panel and not move the scrollbar.

If the above doesnt work automatically, then based on this site, you could do this for a work around:

  private Point mPreviousScrollPosition = new Point();

  public void Form1_Deactivate(object sender, EventArgs e)
  {
     mPreviousScrollPosition = outer_most_panel.AutoScrollPosition;
  }

  public void Form1_Activated(object sender, EventArgs e)
  {
     mPreviousScrollPosition.X *= -1;
     mPreviousScrollPosition.Y *= -1;
     outer_most_panel.AutoScrollPosition = mPreviousScrollPosition;
  }
SwDevMan81
As you can observe in designer, the actual form contains already 2 panels - little panel(scrollPanel) with AutoScroll=true and the big panel (childPanel) with the controls.
serhio
I added a work around that you can use for the out most panel of your GUI. This also worked for me.
SwDevMan81
could I "tell" to the panel do NOT move the focus to the inner control, but to keep focus on itself when it receives the focus? I have this problem in an other non-resolved thread: http://stackoverflow.com/questions/2012626/help-setting-focus-on-the-parent
serhio
This is a bit tricky, since you have to change the way the control sets its ActiveControl property. I'll see if I can figure some way to do this.
SwDevMan81
now, the above question was resolved (http://stackoverflow.com/questions/2012626/help-setting-focus-on-the-parent), so need I to move the 0-sized focus panel with the AutoScrollPosition?
serhio