How to capture mouse wheel on panel in C#? I'm using WinForms
EDIT:
I try to do it on PictureBox
now.
My code:
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick); this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick); private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { MessageBox.Show("Click"); }
Clicking works. Wheelling doesn't. Why?
Go to the normal mouse tab, add a new button, go to the "click here to select mouse button" area and scroll the wheel. It will capture that action and you may assign it to what you want.
Move your fingers between the top and bottom of your touchpad to scroll up and down, or move your fingers across the touchpad to scroll sideways. Be careful to space your fingers a bit apart. If your fingers are too close together, they just look like one big finger to your touchpad.
A mouse with a rubber or plastic wheel located between the left and right buttons (the "scroll wheel"). Also known as a "wheel mouse," when the wheel is moved back and forth, the active window is scrolled, eliminating the need to aim the pointer (cursor) at the scroll bar.
If you can't see the "MouseWheel" event on a component, then you need to create it manually. Also, we need to focus that component, otherwise the "MouseWheel" event will not work for that component. I will show you how to create a "MouseWheel" event for "pictureBox1" and how it works.
INSIDE THE CONSTRUCTOR, create a mousewheel event on that component.
InitializeComponent(); this.pictureBox1.MouseWheel += pictureBox1_MouseWheel;
CREATE THE FUNCTION manually. According to my example, call it "pictureBox1_MouseWheel"
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { //you can do anything here }
CREATE a MouseHover event on that component (Go to properties in PicureBox1, select event, locate "MouseHover" and double-click the "MouseHover" event).
CALL "Focus()"; method inside that MouseHover event.
pictureBox1.Focus();
Now run the program.
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