Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - how do I prevent mousewheel-scrolling in my combobox?

I have a combobox and I want to prevent the user from scrolling through the items with the mousewheel.

Is there an easy way to do that?

(C#, VS2008)

like image 903
Pygmy Avatar asked Dec 10 '09 18:12

Pygmy


2 Answers

Use the MouseWheel event for your ComboBox:

void comboBox1_MouseWheel(object sender, MouseEventArgs e) {     ((HandledMouseEventArgs)e).Handled = true; } 

Note: you'll have to create event in code:

comboBox1.MouseWheel += new MouseEventHandler(comboBox1_MouseWheel); 
like image 80
Jay Riggs Avatar answered Sep 17 '22 05:09

Jay Riggs


For WPF, handle the PreviewMouseWheel event instead.

It would also be a good idea to consider ComboBox.IsDropDownOpen so the user can still use mouse scroll if there are a lot of items in the selection when the ComboBox is expanded.

Another thing is to apply the same behavior across the whole application.

I usually do all the above using the following code:

App.xaml

<Application.Resources>     <Style TargetType="ComboBox">         <EventSetter Event="PreviewMouseWheel" Handler="ComboBox_PreviewMouseWheel" />     </Style> </Application.Resources> 

App.xaml.cs

private void ComboBox_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) {     e.Handled = !((System.Windows.Controls.ComboBox)sender).IsDropDownOpen; } 
like image 30
Jan Paolo Go Avatar answered Sep 21 '22 05:09

Jan Paolo Go