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();
}
}
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With