|
-
Dec 13th, 2022, 08:44 AM
#1
Thread Starter
Junior Member
[RESOLVED] Prevent AutoScroll from jumping to the top
I’ve got a custom control derived from System.Windows.Forms.UserControl, which paints itself in an overridden OnPaint proc and also has a varying number of other controls sitting on top. Depending on user input this custom control might grow larger than can be displayed in its parent form.
I set AutoScroll=True and VerticalScroll.Visible = True for the custom control to enable scrolling. This works as expected.
However, after any other window got the focus and then the focus is reset to my form with the custom control again, the control will automatically scroll to the very top of the scrollbar. – This is very annoying as users needs to reposition the scroll bar to where they were before.
How can I prevent AutoScroll from jumping to the top when the form gets the focus?
(PS: Target Framework is still .NET Framework 2.0.)
-
Dec 13th, 2022, 02:11 PM
#2
Thread Starter
Junior Member
Re: Prevent AutoScroll from jumping to the top
I've now tried several approaches, including...
- overwriting the WndProc and swallowing scroll-related events,
- storing the position of the scrollbar on Deactivate and resetting it to the same position on Activate,
- making the child control closest to the correct position the active control.
The latter two of these improved the problematic behavior but none was a real solution due to remaining flaws.
The last of the above approaches however gave me an idea. Instead of trying to make an existing control the reference point for repositioning, I overrode the ScrollToControl function of my custom control to return the exact location of the current DisplayRectangle.
Code:
Protected Overrides Function ScrollToControl(activeControl As Control) As Point
Return Me.DisplayRectangle.Location
End Function
This is a flawless solution for my problem!
Thanks to everybody reading this thread and thinking about a solution to this problem!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|