How to disable mousewheel for one control i.e. Combo Box?
Add an event handler to your code
private void anycontrol_MouseEnter(object sender, System.EventArgs e)
{
var senderControl = sender as System.Windows.Forms.Control;
if(senderControl==null)
return;
senderControl.Focus();
}
And assign it to any controls you want your focus to be applied automatically.
somePanel.MouseEnter += new System.EventHandler(anycontrol_MouseEnter);
//or this way
somePanel.MouseEnter += anycontrol_MouseEnter;
someComboBox.MouseEnter += anycontrol_MouseEnter;
edit:
Including the details you provided, I'd do it this way:
bool AllowUsersScrolling;
private void usersCombobox_MouseLeave(object sender, System.EventArgs e)
{
AllowUsersScrolling = false;
}
private void usersCombobox_MouseEnter(object sender, System.EventArgs e)
{
AllowUsersScrolling = true;
}
private void usersCombobox_MouseWheel(object sender, MouseEventArgs e)
{
if(!AllowUsersScrolling)
((HandledMouseEventArgs)e).Handled = true;
}
And attach those handlers to your control's events respectively.
How to prevent Mouse Scroll in ToolStripComboBox?
The ToolStripComboBox
helpfully exposes its underlying ComboBox
control in its aptly named ComboBox
property. This allows us to access its properties, methods, and events that were not been wrapped into the ToolStripComboBox
.
And, as you probably know, the standard ComboBox
control exposes a MouseWheel
event 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 ToolStripComboBox
control's underlying ComboBox
control's MouseWheel
event, and override its default behavior.
So, assuming you have a form that contains a ToolStrip
and a ToolStripComboBox
, you can use something like the following code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Attach a handler for the MouseWheel event
ToolStripComboBox1.ComboBox.MouseWheel += new MouseEventHandler(ToolStripComboBox_MouseWheel);
}
private void ToolStripComboBox_MouseWheel(object sender, MouseEventArgs e)
{
//Cast the MouseEventArgs to HandledMouseEventArgs
HandledMouseEventArgs mwe = (HandledMouseEventArgs)e;
//Indicate that this event was handled
//(prevents the event from being sent to its parent control)
mwe.Handled = true;
}
}
Alternatively, of course, you could always subclass the existing ToolStripComboBox
control and override its behavior there in the same way shown above.
How to keep ComboBox from scrolling? C#
combobox.MouseWheel += new MouseEventHandler(combobox_MouseWheel);
void combobox_MouseWheel(object sender, MouseEventArgs e)
{
((HandledMouseEventArgs)e).Handled = true;
}
Disable scrolling for mouse-over in ComboBox (WPF)
You could handle the RequestBringIntoView
event for the ComboBoxItem
containers:
<ComboBox>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<EventSetter Event="RequestBringIntoView" Handler="ComboBox_RequestBringIntoView"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
private void ComboBox_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
Related Topics
Getting Selected Value of a Combobox
How to Render Encoded Tags as Proper HTML, Rather Than Text
Why Does My C# Array Lose Type Sign Information When Cast to Object
Sending a Value from Server to Client with Sockets
C#/.Net Analysis Tool to Find Race Conditions/Deadlocks
Creating Simple C++.Net Wrapper. Step-By-Step
ASP.NET MVC 4 + Ninject MVC 3 = No Parameterless Constructor Defined for This Object
Regex:How to Get Words from a String (C#)
Is Correct to Use Gc.Collect(); Gc.Waitforpendingfinalizers();
How Are Dlls Loaded by the Clr
Add the Where Clause Dynamically in Entity Framework
How to Protect This Function from SQL Injection
Entity Framework Initialization Is Slow -- What How to Do to Bootstrap It Faster
Why Can't I Reference System.Componentmodel.Dataannotations
C# Compiler Bug? Why Doesn't This Implicit User-Defined Conversion Compile