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.
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).
Set AutoScroll
to true
panel1.AutoScroll = true;
And, then in Details form's load
event, set the VerticalScroll
panel1.VerticalScroll.Value = 0;
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With