Disable MouseWheel in RadSpinElement

4 posts, 1 answers
  1. Jason
    Jason avatar
    14 posts
    Member since:
    Oct 2011

    Posted 10 Oct 2012 Link to this post

    I've dynamically added a RadSpinElement to my RadRibbonBar and I'd like to make it so that the MouseWheel scrolling does not affect the value in the textbox. I'd like the user to have to either hold down the up/down arrow or type in a value.

    Is there an event or property I can use to achieve this?
  2. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 11 Oct 2012 Link to this post

    Hi Jason,

    Thank you for writing.

    You should set the RadSpinElement EnableMouseWheel property that gets or sets a value indicating whether the user can change the value with mouse wheel.

    I hope this helps.

    Kind regards,
    Peter
    the Telerik team
    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jason
    Jason avatar
    14 posts
    Member since:
    Oct 2011

    Posted 11 Oct 2012 Link to this post

    Is that property new in the 2012 Q2 release? I'm using the Q1 release and I don't see that property available - however I do see it in the online documentation.
  5. Svett
    Admin
    Svett avatar
    728 posts

    Posted 12 Oct 2012 Link to this post

    Hi Jason,

    The property will be available in the next major release, which will be available by the end of the next week. Nevertheless, you can create a custom editor to achieve the desired scenario. You can use the following code snippet:
    public class MyGridSpinEditorElement : GridSpinEditorElement
        {
            private static readonly MethodInfo MouseWheelMethodInfo;
     
            static MyGridSpinEditorElement()
            {
                MouseWheelMethodInfo = typeof(RadSpinElement).GetMethod("textItem_MouseWheel", BindingFlags.Instance | BindingFlags.NonPublic);
            }
     
            private MouseEventHandler SpinElementMouseWheel;
            private bool enableMouseWheel = true;
     
            public MyGridSpinEditorElement()
            {
     
            }
     
            protected override void WireEvents()
            {
                base.WireEvents();
     
                if (this.SpinElementMouseWheel == null)
                {
                    this.SpinElementMouseWheel = Delegate.CreateDelegate(typeof(MouseEventHandler), this, MouseWheelMethodInfo) as MouseEventHandler;
                }
     
                this.TextBoxItem.MouseWheel -= this.SpinElementMouseWheel;
                this.TextBoxItem.HostedControl.MouseWheel -= this.SpinElementMouseWheel;
                this.TextBoxItem.MouseWheel += new MouseEventHandler(TextBoxItem_MouseWheel);
                this.TextBoxItem.HostedControl.MouseWheel += new MouseEventHandler(TextBoxItem_MouseWheel);
            }
     
            protected override void UnwireEvents()
            {
                base.UnwireEvents();
                this.TextBoxItem.MouseWheel -= new MouseEventHandler(TextBoxItem_MouseWheel);
                this.TextBoxItem.HostedControl.MouseWheel -= new MouseEventHandler(TextBoxItem_MouseWheel);
            }
     
            public bool EnableMouseWheel
            {
                get { return this.enableMouseWheel; }
                set { this.enableMouseWheel = value; }
            }
     
            private void TextBoxItem_MouseWheel(object sender, MouseEventArgs e)
            {
                if (this.enableMouseWheel)
                {
                    this.SpinElementMouseWheel(sender, e);
                }
            }
        }

    public class MyGridSpinEditor : GridSpinEditor
    {
        protected override Telerik.WinControls.RadElement CreateEditorElement()
        {
            MyGridSpinEditorElement element = new MyGridSpinEditorElement();
            element.EnableMouseWheel = false;
            return element;
        }
    }

    Then you should use the EditorRequired event to replace the default one:
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (e.EditorType == typeof(GridSpinEditor))
        {
            e.EditorType = typeof(MyGridSpinEditor);
        }
    }
    Regards,
    Svett
    the Telerik team
    You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
Back to Top