locked
Disabling mouse wheel in ComboBox? RRS feed

  • Question

  • Hello,

    How is possible to disable mouse wheel in combobox control?

    I implemented new ComboBox component that derives from ComboBox and override OnMouseWheel event handler (I don't call base.OnMouseWheel(e) in it) and it doesn't work when I add this new component to the Form (scrolling with wheel is still possible). Any solutions?
    Tuesday, April 22, 2008 1:23 PM

Answers

  •  cya wrote:
    Rudedog2, thank you for help but seems it doesn't work. Still the same.

    If the same is done for the NumericUpDown control (new component with overriden OnMouseWheel method, without calling base method) it works fine. So why is difference between ComboBox and NumericUpDown?

     

    Proven and tested.

     

    Code Snippet

    public class NoWheelComboBox : ComboBox

    {

    int selectedIndex = 0;

    bool onMouseWheel = false;

     

    public NoWheelComboBox()

    {

    // TODO: NoWheelComboBox constructor

    }

    protected override void OnMouseWheel(MouseEventArgs e)

    {

    onMouseWheel = true;

    selectedIndex = this.SelectedIndex;

    MouseEventArgs eMouse =

    new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, 0);

    base.OnMouseWheel(eMouse);

    }

    protected override void OnTextChanged(EventArgs e)

    {

    // This base class method still responds to MouseWheel

    base.OnTextChanged(e);

    if (onMouseWheel)

    {

    this.SelectedIndex = selectedIndex;

    }

    else

    {

    onMouseWheel = false;

    }

    }

    }

     

 

Wow.  That other code did not work on a combobox, but yet it worked on other types of controls.  I would consider that to be a bug.  I had success with this work around.

 

Rudedog

 

 

Wednesday, April 23, 2008 2:34 PM
  • Thank you all for replies. Only solution that works is:

    Code Snippet

        protected override void WndProc(ref Message m)
        {
          if (m.Msg == 522)     /* WM_MOUSEWHEEL */
          {
            return;
          }

          base.WndProc(ref m);
        }




    Thursday, April 24, 2008 12:58 PM
  • All replies

    •  cya wrote:
      Hello,

      How is possible to disable mouse wheel in combobox control?

      I implemented new ComboBox component that derives from ComboBox and override OnMouseWheel event handler (I don't call base.OnMouseWheel(e) in it) and it doesn't work when I add this new component to the Form (scrolling with wheel is still possible). Any solutions?

       

      Try this with your ComboBox.OnMouseWheel override method.

       

      Code Snippet

      protected override void OnMouseWheel(MouseEventArgs e)

      {

      MouseEventArgs eMouse =

      new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, 0);

       

      base.OnMouseWheel(eMouse);

      }

       

       

      Hope this helps.

       

      Rudedog

      Tuesday, April 22, 2008 2:58 PM
  • Rudedog2, thank you for help but seems it doesn't work. Still the same.

    If the same is done for the NumericUpDown control (new component with overriden OnMouseWheel method, without calling base method) it works fine. So why is difference between ComboBox and NumericUpDown?
    Wednesday, April 23, 2008 6:13 AM
  •  cya wrote:
    Rudedog2, thank you for help but seems it doesn't work. Still the same.

    If the same is done for the NumericUpDown control (new component with overriden OnMouseWheel method, without calling base method) it works fine. So why is difference between ComboBox and NumericUpDown?

     

    Proven and tested.

     

    Code Snippet

    public class NoWheelComboBox : ComboBox

    {

    int selectedIndex = 0;

    bool onMouseWheel = false;

     

    public NoWheelComboBox()

    {

    // TODO: NoWheelComboBox constructor

    }

    protected override void OnMouseWheel(MouseEventArgs e)

    {

    onMouseWheel = true;

    selectedIndex = this.SelectedIndex;

    MouseEventArgs eMouse =

    new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, 0);

    base.OnMouseWheel(eMouse);

    }

    protected override void OnTextChanged(EventArgs e)

    {

    // This base class method still responds to MouseWheel

    base.OnTextChanged(e);

    if (onMouseWheel)

    {

    this.SelectedIndex = selectedIndex;

    }

    else

    {

    onMouseWheel = false;

    }

    }

    }

     

  •  

    Wow.  That other code did not work on a combobox, but yet it worked on other types of controls.  I would consider that to be a bug.  I had success with this work around.

     

    Rudedog

     

     

    Wednesday, April 23, 2008 2:34 PM
  • Rudedog2, thank you again for help.

    This code snippet really disables the wheel in combobox
    , but has one big bug. If you select entry in combobox and then use wheel, selected entry will be locked and you haven't got chance to select another entry.

    Any solution for this issue?

    Thursday, April 24, 2008 7:12 AM
  • Try out this

    Code Snippet

    public class NoWheelComboBox : ComboBox

    {

    int selectedIndex = 0;

    bool onMouseWheel = false;

     

     

    public NoWheelComboBox()

    {

    // TODO: NoWheelComboBox constructor

    }

    protected override void OnMouseWheel(MouseEventArgs e)

    {

    onMouseWheel = true;

    selectedIndex = this.SelectedIndex;

    MouseEventArgs eMouse =

    new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, 0);

    base.OnMouseWheel(eMouse);

    }

    protected override void OnTextChanged(EventArgs e)

    {

    // This base class method still responds to MouseWheel

    base.OnTextChanged(e);

    if (onMouseWheel)

    {

    this.SelectedIndex = selectedIndex;

    onMouseWheel = false;

    }

    }

    }

     

     

    Thursday, April 24, 2008 7:57 AM
  • Thank you all for replies. Only solution that works is:

    Code Snippet

        protected override void WndProc(ref Message m)
        {
          if (m.Msg == 522)     /* WM_MOUSEWHEEL */
          {
            return;
          }

          base.WndProc(ref m);
        }




    Thursday, April 24, 2008 12:58 PM
  • How would you do this is vb.net?

     

    Thank you very much!

     

    Thursday, April 24, 2008 7:18 PM
  •  cya wrote:
    Rudedog2, thank you again for help.

    This code snippet really disables the wheel in combobox
    , but has one big bug. If you select entry in combobox and then use wheel, selected entry will be locked and you haven't got chance to select another entry.

    Any solution for this issue?

    I tested that with VS2005 Standard and do not have that problem. 

    I filled my items collection using the designer.

    I tested before I posted. 

    I can select using the drop-down, and yes the wheel is locked.

    I can use the drop-down to re-select, and the wheel is still locked.

     

    Happy Coding.

     

    Rudedog

    Thursday, April 24, 2008 7:23 PM
  • Hello cya,

    Your solution works, but too well as it also disables mouse wheel scrolling if vert scroll bar is visible when the combo box is dropped. To handle that situation, add the following code:

     
        protected bool is_dropped = false;
    
        private const int WM_MOUSEWHEEL = 0x020A;
    
        protected override void OnDropDown(EventArgs e)
        {
          is_dropped = true;
          base.OnDropDown(e);
        }
    
        protected override void OnDropDownClosed(EventArgs e)
        {
          is_dropped = false;
          base.OnDropDownClosed(e);
        }
    
        protected override void WndProc(ref Message m)
        {
          if (m.Msg == WM_MOUSEWHEEL && !is_dropped)
            return;
    
          base.WndProc(ref m);
        }

    • Edited by j2associates Saturday, June 25, 2011 1:01 AM fixed typo
    Saturday, June 25, 2011 12:59 AM