|
| 1 | +--- |
| 2 | +title: Adjusting Mouse Wheel Scroll Step in SyntaxEditor for UI for WinForms |
| 3 | +description: Learn how to customize the mouse wheel scroll step in the RadSyntaxEditor control. |
| 4 | +type: how-to |
| 5 | +page_title: Customize Mouse Wheel Scrolling Speed/Step in RadSyntaxEditor |
| 6 | +meta_title: Customize Mouse Wheel Scrolling Speed/Step in RadSyntaxEditor |
| 7 | +slug: syntaxeditor-mouse-wheel-step |
| 8 | +tags: syntaxeditor, scrolling, mouse-wheel, customization |
| 9 | +res_type: kb |
| 10 | +ticketid: 1561729 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2025.2.520|RadSyntaxEditor for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +There could be a scenario in which the mouse wheel speed needs to be per user preferences. However, the control does not provide a way to manipulate the scroll step. In this KB article, we will demonstrate how, using custom code, we could manipulate the scroll speed. |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +To adjust the mouse wheel scroll step, create a custom class that inherits from RadSyntaxEditor and override the `OnMouseWheel` method. Modify the `Value` property of the `VerticalScrollBarElement` and the `VerticalScrollOffset` of the `SyntaxEditorPresenter`. The last step is to set the `VerticalScrollOffset` property. This property is not public and we will need to use reflection to set it. Below is a sample implementation: |
| 26 | + |
| 27 | +````C# |
| 28 | + |
| 29 | +public class MySyntax : RadSyntaxEditor |
| 30 | +{ |
| 31 | + protected override void CreateChildItems(RadElement parent) |
| 32 | + { |
| 33 | + base.CreateChildItems(parent); |
| 34 | + } |
| 35 | + |
| 36 | + protected override void OnMouseWheel(MouseEventArgs e) |
| 37 | + { |
| 38 | + // Access and update scrolling properties |
| 39 | + System.Reflection.PropertyInfo pi = typeof(SyntaxEditorPresenter).GetProperty( |
| 40 | + "VerticalScrollOffset", |
| 41 | + System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public |
| 42 | + ); |
| 43 | + |
| 44 | + if (e.Delta < 0) // Scroll down |
| 45 | + { |
| 46 | + this.SyntaxEditorElement.VerticalScrollBar.Value += this.SyntaxEditorElement.VerticalScrollBar.SmallChange; |
| 47 | + pi.SetValue(this.SyntaxEditorElement.EditorPresenter, this.SyntaxEditorElement.VerticalScrollBar.Value); |
| 48 | + } |
| 49 | + else // Scroll up |
| 50 | + { |
| 51 | + this.SyntaxEditorElement.VerticalScrollBar.Value -= this.SyntaxEditorElement.VerticalScrollBar.SmallChange; |
| 52 | + pi.SetValue(this.SyntaxEditorElement.EditorPresenter, this.SyntaxEditorElement.VerticalScrollBar.Value); |
| 53 | + } |
| 54 | + |
| 55 | + this.SyntaxEditorElement.EditorPresenter.InvalidateLayout(false); |
| 56 | + } |
| 57 | + public override string ThemeClassName |
| 58 | + { |
| 59 | + get |
| 60 | + { |
| 61 | + return typeof(RadSyntaxEditor).FullName; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +```` |
| 67 | + |
| 68 | +### Steps: |
| 69 | + |
| 70 | +1. Create a new class that inherits `RadSyntaxEditor`. |
| 71 | +2. Override the `OnMouseWheel` method. |
| 72 | +3. Use reflection to access and modify the `VerticalScrollOffset` property of `SyntaxEditorPresenter`. |
| 73 | +4. Update the `Value` property of `VerticalScrollBarElement` based on the mouse wheel scroll direction. |
| 74 | +5. Test the custom implementation by adding it to your application. |
| 75 | + |
| 76 | +## See Also |
| 77 | +* [RadSyntaxEditor Documentation](https://docs.telerik.com/devtools/winforms/controls/syntaxeditor/overview) |
0 commit comments