public class ScrollHelper { XtraScrollableControl scrollableControl; public ScrollHelper(XtraScrollableControl scrollableControl) { this.scrollableControl = scrollableControl; } public void EnableScrollOnMouseWheel() { scrollableControl.VisibleChanged += OnVisibleChanged; } void OnVisibleChanged(object sender, EventArgs e) { scrollableControl.Select(); UnsubscribeFromMouseWheel(scrollableControl.Controls); SubscribeToMouseWheel(scrollableControl.Controls); } private void SubscribeToMouseWheel(Control.ControlCollection controls) { foreach (Control ctrl in controls) { ctrl.MouseWheel += OnMouseWheel; SubscribeToMouseWheel(ctrl.Controls); } } private void UnsubscribeFromMouseWheel(Control.ControlCollection controls) { foreach (Control ctrl in controls) { ctrl.MouseWheel -= OnMouseWheel; UnsubscribeFromMouseWheel(ctrl.Controls); } } void OnMouseWheel(object sender, MouseEventArgs e) { DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true; int scrollValue = scrollableControl.VerticalScroll.Value; int largeChange = scrollableControl.VerticalScroll.LargeChange; if (e.Delta < 0) scrollableControl.VerticalScroll.Value += scrollableControl.VerticalScroll.LargeChange; else if (scrollValue < largeChange) scrollableControl.VerticalScroll.Value = 0; else scrollableControl.VerticalScroll.Value -= largeChange; } public void DisableScrollOnMouseWheel() { scrollableControl.VisibleChanged -= OnVisibleChanged; UnsubscribeFromMouseWheel(scrollableControl.Controls); scrollableControl = null; } } |