Search code examples
c#winformsevent-handling

MouseWheel Event On ComboBox Triggers KeyDown Event


Create a Windows Form (Net Framework or Net Core) and add a Toolstrip with ToolStripComboBox control. And then add these methods:

public Form1()
{
    InitializeComponent();
    KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    //any code here;
}

If Form KeyPreview = false then Form1_KeyDown event is not triggered with MouseWheel event on ToolstripComboBox. Setting KeyPreview = true (is necessary) causes MouseWheel event is captured by Form1_KeyDown handler as Up or Down.

Is there a way to prevent Form_KeyDown handler Not To Handle MouseWheel event?


Solution

  • both using comments here as source and referencing vs code knowledge, one solution to this:

    private void Form1_KeyDown(dynamic sender, KeyEventArgs e)
    {
        if (sender.GetType().Name != "Form1")
        {
            //code for action...
        }
    }