MouseWheel event doesn't fire when using any control with scrolbars (in C# Windows Forms)
Asked Answered
T

4

14

MouseWheel event doesn't fire when I' am using any control (ListBox, Panel, TextBox) with scrollbars.

To reproduce problem:

public class Form1 : Form
 {
  private readonly Button button1;
  private readonly TextBox textBox1;

  private void button1_MouseWheel(object sender, MouseEventArgs e)
  {
   ToString(); // doesn't fire when uncomment lines below
  }

  public Form1()
  {
   button1 = new Button();
   textBox1 = new TextBox();
   SuspendLayout();

   button1.Location = new System.Drawing.Point(80, 105);
   button1.Size = new System.Drawing.Size(75, 23);
   button1.MouseWheel += button1_MouseWheel;
   button1.Click += button1_Click;

   textBox1.Location = new System.Drawing.Point(338, 105);
   //textBox1.Multiline = true; // uncomment this
   //textBox1.ScrollBars = ScrollBars.Vertical;  // uncomment this 
   textBox1.Size = new System.Drawing.Size(100, 92);

   ClientSize = new System.Drawing.Size(604, 257);
   Controls.Add(textBox1);
   Controls.Add(button1);
   ResumeLayout(false);
   PerformLayout();
  }

  // Clicking the button sets Focus, but even I do it explicit Focus() or Select()
  // still doesn't work
  private void button1_Click(object sender, System.EventArgs e)
  {
   button1.Focus();
   button1.Select();
  }
 }
Thatcher answered 19/1, 2010 at 15:3 Comment(4)
Maybe the problem is in the mouse, I use "Lenovo USB Optical Wheel Mouse" (default configuration "Whell Button" function "Quick/AutoScroll")Thatcher
I use: Windows Vista Buisness, .Net Framework 3.5 SP1, Visual Studio 2008 Standard Edition (all official hotfixes). Build Debug: Target Framework = .Net Framework 3.5, Output type = Windows ApplicationThatcher
Mouse Configuration: Control Panel/Mouse/Wheel/Whell->Enable Universal Scrolling; Control Panel/Mouse/Wheel/Whell Button->Quick/AutoScrol; Control Panel/Mouse/Wheel/Scrolling Speed->Scroll=10 line(s) per noth;Thatcher
I'm struggling with the same problem. I think many people confuse it with the "focused vs. hovered over" problematics. I have same ScrollableControl-based control which either triggers OnMouseWheel or doesn't, depending on whether it is displaying scrollbars currently or not. I think scrollbars somehow capture the wheel events, but I didn't study the issue enough yet. Did OP find a solution by any chance?Ctesiphon
T
2

I found solution, gility is default "Mouse Configuration". Lenovo USB Optical Wheel Mouse default configuration is:

Control Panel/Mouse/Wheel/Whell->Enable Universal Scrolling;

I changed to:

Control Panel/Mouse/Wheel/Whell->Use Microsoft Office 97 Scrolling Emulation Only

Now in .net code MouseWheel working with Focused Control.


But questions are:

  • how can I fix it in .net code?
  • how can I detect this situation in .net code?

Any ideas ?

Thatcher answered 20/1, 2010 at 8:26 Comment(1)
Use Spy++ and check which messages are being sent to the controls.Lipophilic
A
15

I was having the same problem, and what worked for me was to add a handler for the event MouseEnter in the control, that one is triggered with or without focus.

private void chart1_MouseEnter(object sender, EventArgs e)
{
    chart1.Focus();
}

After that I could get the mouseWheel events with no problems.

Anachronistic answered 13/2, 2012 at 19:36 Comment(2)
Strangely, on one W10 compu the identical program did mousewheel, on the other it didn't, until I used your solution.Mitigate
I have notified similar discrepancies between Windows 7 and 10 : the former will send mouse wheel events to active control only (eg: combobox) while second will send those events to control which is under mouse (eg: panel). Additionally, In Windows 7 if no control has focus, mouse wheel events are sent to main form.Waitabit
O
2

You normally need to make sure the control you want to handle the MouseWheel event is active.

For example try calling button1.Select() in the Form Load (or Shown) event and then using the scroll wheel.

eg:

private void Form1_Load(object sender, EventArgs e)
{
    button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);

    button1.Select();  
}
Organzine answered 19/1, 2010 at 15:9 Comment(1)
Still nothing, same problem,it is very strange, but in my case it doesn't work. Even I set Focus or Select, and Cursor is over the buton1, when I use mouse wheel, it scrolls textbox1 (sic!)Thatcher
T
2

I found solution, gility is default "Mouse Configuration". Lenovo USB Optical Wheel Mouse default configuration is:

Control Panel/Mouse/Wheel/Whell->Enable Universal Scrolling;

I changed to:

Control Panel/Mouse/Wheel/Whell->Use Microsoft Office 97 Scrolling Emulation Only

Now in .net code MouseWheel working with Focused Control.


But questions are:

  • how can I fix it in .net code?
  • how can I detect this situation in .net code?

Any ideas ?

Thatcher answered 20/1, 2010 at 8:26 Comment(1)
Use Spy++ and check which messages are being sent to the controls.Lipophilic
L
0

I tried your example, and, whether the lines were commented or not, the MouseWheel event only fires if the button is focused. This behavior is by design. (the MouseWheel event, like keyboard events, goes to the focused control)

Lipophilic answered 19/1, 2010 at 15:16 Comment(1)
I read about i documentation but, it is very strange, but in my case it doesn't work: I add to code: public Form1() { (...) button1.Click += button1_Click; (...) } private void button1_Click(object sender, System.EventArgs e) { button1.Focus(); } Not working, even I set Focus, and Cursor is over the buton1, when I use mouse wheel, it scrolls textbox1 (sic!)Thatcher

© 2022 - 2024 — McMap. All rights reserved.