I am trying to disable Mouse Scroll in my ToolStripComboBox, I thought I can catch the Mouse Wheel event, but while the event is available for ComboBox, it is not available instead for the ToolStripComboBox. Any ideas?
How to prevent Mouse Scroll in ToolStripComboBox?
3.7k Views Asked by raz3r At
2
There are 2 best solutions below
0
On
In addition to what Cody Gray suggested you may want the wheel scolling to work when the combobox is dropped. Just chech the DroppedDown state in MouseWheel handle:
public class MyToolStripComboBox : ToolStripComboBox
{
public MyToolStripComboBox()
{
this.ComboBox.MouseWheel += new MouseEventHandler(ComboBox_MouseWheel);
}
void ComboBox_MouseWheel(object sender, MouseEventArgs e)
{
if (!this.ComboBox.DroppedDown)
((HandledMouseEventArgs)e).Handled = true;
}
}
Scrolling dropped combobox with wheel doesn't change the selection, so it works as expected
The
ToolStripComboBoxhelpfully exposes its underlyingComboBoxcontrol in its aptly namedComboBoxproperty. This allows us to access its properties, methods, and events that were not been wrapped into theToolStripComboBox.And, as you probably know, the standard
ComboBoxcontrol exposes aMouseWheelevent that fires each time the mouse wheel is scrolled while the combo box has focus.Putting these two things together, we can add a handler for the
ToolStripComboBoxcontrol's underlyingComboBoxcontrol'sMouseWheelevent, and override its default behavior.So, assuming you have a form that contains a
ToolStripand aToolStripComboBox, you can use something like the following code:Alternatively, of course, you could always subclass the existing
ToolStripComboBoxcontrol and override its behavior there in the same way shown above.