1

I have a WindowsForms UI in C#. i have a Panel and a PictureBox in it. I simply get Mouse Wheel event by form and then zoom PictureBox in panel

    public MainWindow()
    {
        InitializeComponent();
        this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.zoom_handler);// Capture Mouse wheel event
    }

the problem is that when i use a Trackbar, Trackbar gets the mouseWheel event and i can't zoom image anymore. I can't release it by click on PictureBox or Panel. Now what i must do?

Mokhabadi
  • 302
  • 2
  • 14

2 Answers2

2

Try to disable mouseWheel of the trackbar in this way:

trackBar1.MouseWheel += new MouseEventHandler(Disable_MouseWheel);



private void Disable_MouseWheel(object sender, EventArgs e)
{
    HandledMouseEventArgs ee = (HandledMouseEventArgs)e;
    ee.Handled = true;
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • Thanks for your answer but unfortunately it doesn't work. there is other ways to disable it like [This](http://stackoverflow.com/a/34928925/4110655) but it disable mousewheel event not give it to form. – Mokhabadi May 20 '17 at 08:32
  • Should ee.Handled be true or false? – Snympi May 20 '17 at 08:39
  • 1
    @Snympi true, if it is false default MouseWheel handlers will work – Samvel Petrosov May 20 '17 at 08:40
  • Agreed, had to check the MSDN docs to make sure. Naming is a bit counter intuitive. – Snympi May 20 '17 at 08:47
  • @Snympi, this does not contradict intuition, if it is true, this means that the Event has already been handled and there is no need for a handling with default handlers. – Samvel Petrosov May 20 '17 at 08:50
1

Finally i found the answer.

    public MainWindow()
    {
        InitializeComponent();

        this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.zoom_handler);// Capture Mouse wheel event
        mytrackbar.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.zoom_handler);// Capture Mouse wheel event
        mytrackbar.MouseWheel += (sender, e) => ((HandledMouseEventArgs)e).Handled = true;
    }

if you like to disable trackbar scroll with mousewheel, last line is enough. but with that line you disable mousewheel event until you focus on other control that has mousewheel event. if you use mousewheel event on some control (or probably form) that can't simply get focus, you must call your function on trackbar mousewheel event.

Mokhabadi
  • 302
  • 2
  • 14