|
| 1 | +--- |
| 2 | +title: Preventing Scrollbars from Moving When Clicking on the ScrollablePanel |
| 3 | +description: Learn how to stop the automatic scrolling behavior of RadScrollablePanel to the focussed control inside. |
| 4 | +type: how-to |
| 5 | +page_title: How to Stop Scrollbars from Moving in RadDock for WinForms Windows |
| 6 | +slug: scrollablepanel-prevent-scrollbars-moving |
| 7 | +tags: panelsandlabels, scrollbar, auto-scroll, custom-class |
| 8 | +res_type: kb |
| 9 | +ticketid: 1663867 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +|Product Version|Product|Author| |
| 15 | +|----|----|----| |
| 16 | +|2024.3.806|RadGridView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 17 | + |
| 18 | +## Description |
| 19 | +When clicking on the content of the RadScrollablePanel, the scrollbars automatically move to make the focused control visible. This behavior is due to the `RadScrollablePanel` automatically scrolling to the active control. The goal is to disable this automatic scrolling behavior so that the scrollbars do not move when a window is clicked. |
| 20 | + |
| 21 | +## Solution |
| 22 | +To prevent the scrollbars from moving when clicking on a window, you need to override the `ScrollToControl` method of the `RadScrollablePanelContainer`. This involves creating a custom class that derives from `RadScrollablePanelContainer` and then using this custom container in your `RadScrollablePanel`. |
| 23 | + |
| 24 | +First, create a custom `RadScrollablePanelContainer`: |
| 25 | + |
| 26 | +````C# |
| 27 | +public class MyRadScrollableContainer : RadScrollablePanelContainer |
| 28 | +{ |
| 29 | + private RadScrollablePanel myParentPanel; |
| 30 | + |
| 31 | + public MyRadScrollableContainer() : base(null) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + public MyRadScrollableContainer(RadScrollablePanel parentPanel) : base(parentPanel) |
| 36 | + { |
| 37 | + this.DoubleBuffered = true; |
| 38 | + this.SetStyle(ControlStyles.Selectable, true); |
| 39 | + this.myParentPanel = parentPanel; |
| 40 | + } |
| 41 | + |
| 42 | + protected override Point ScrollToControl(Control activeControl) |
| 43 | + { |
| 44 | + Point result = Point.Empty; |
| 45 | + if (myParentPanel != null) |
| 46 | + { |
| 47 | + result = new Point(-this.myParentPanel.HorizontalScrollbar.Value, -this.myParentPanel.VerticalScrollbar.Value); |
| 48 | + } |
| 49 | + return result; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +```` |
| 54 | + |
| 55 | +Then, create a custom `RadScrollablePanel` that uses your custom container: |
| 56 | + |
| 57 | +````C# |
| 58 | +public class CustomRadScrollablePanel : RadScrollablePanel |
| 59 | +{ |
| 60 | + protected override RadScrollablePanelContainer CreateScrollablePanelContainer() |
| 61 | + { |
| 62 | + return new MyRadScrollableContainer(this); |
| 63 | + } |
| 64 | + |
| 65 | + public override string ThemeClassName |
| 66 | + { |
| 67 | + get |
| 68 | + { |
| 69 | + return typeof(RadScrollablePanel).FullName; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +```` |
| 75 | + |
| 76 | +Finally, replace the default `RadScrollablePanel` with your custom version in the applicable part of your application, such as the `OnWindowCreate()` method. |
| 77 | + |
| 78 | +By implementing this custom container, you disable the default behavior that automatically scrolls to the active control, thus preventing the scrollbars from moving when a window is clicked. |
| 79 | + |
| 80 | +## See Also |
| 81 | +- [RadDock Overview](https://docs.telerik.com/devtools/winforms/controls/dock/overview) |
| 82 | +- [Customizing RadScrollablePanel Behavior](https://docs.telerik.com/devtools/winforms/controls/panels/scrollable-panel/customizing-behavior) |
0 commit comments