Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset the panel scroll position in winform application c#

Tags:

c#

winforms

I'm working on winfom application c#. I have two forms called Welome and Details. Details contains 7 grids in the Panel.

Scenario: If I click on any of the item on welcome page it will take to the Details page with seven grids. If I drag the scroll bar down, and come back after moving back to welcome form, still the scroll bar stays at the same position.

Question: I want to reset the scroll position to top each time the user visits the details form, so that I can always see first grid.

like image 860
Ram Avatar asked Jun 14 '12 09:06

Ram


People also ask

How can I see ScrollBar position?

The scrollbar position along a horizontal and vertical axis is denoted by integers. The pageXOffset property returns the number of pixels scrolled along the horizontal axis (i.e. left and right) and the pageYOffset property returns the number of pixels scrolled along the vertical axis (i.e. top and bottom).


3 Answers

Set AutoScroll to true

panel1.AutoScroll = true;

And, then in Details form's load event, set the VerticalScroll

panel1.VerticalScroll.Value = 0;
like image 81
Angshuman Agarwal Avatar answered Oct 18 '22 20:10

Angshuman Agarwal


If Angshuman Agarwal's answer doesn't work for you, the culprit is likely that after load some control in the form is receiving focus, which will scroll into view and override any changes to the scroll position.

You could set TabStop to false, but then your form wouldn't be tabbable :(

A clunky work-around, but still relatively simple fix, is manually fire focus on the first control in your form:

yourFirstControl1.Select();

See also: How to make the panel scroll bar to be at the TOP position on loading the form

like image 33
KyleMit Avatar answered Oct 18 '22 20:10

KyleMit


Old post, but still relevant. The above only worked when I added a line:

displayPanel.AutoScroll = true;
displayPanel.AutoScrollPosition = new Point(displayPanel.AutoScrollPosition.X, 0);
displayPanel.VerticalScroll.Value = 0;

Then it worked great, without having to set any tab indexes.

like image 39
dotnettex Avatar answered Oct 18 '22 19:10

dotnettex