c# Disable ScrollWheel on Trackbar

2019-07-12 20:00发布

问题:

How can I disable the valuechange with mouse wheel on trackbars? When scrolling down on the interface users can change trackbar values by mistake I'm using Windows forms c#. I couldn't find no property to stop this event..

回答1:

I solved the issue with this: With normal event declaration..

Control = new TrackBar();
Control.MouseWheel += Control_MouseWheel;
private void Control_MouseWheel(object sender, MouseEventArgs e)
{
     ((HandledMouseEventArgs)e).Handled = true;
}

Using anonymous method

var Control = new TrackBar();
Control.MouseWheel += new MouseEventHandler(delegate(object sender, MouseEventArgs e)
{
    ((HandledMouseEventArgs)e).Handled = true;
});

What it does is to prevent further execution..

Edit: Using Labda expression as said by Sriram Sakthivel

Control.MouseWheel += (sender, e) =>((HandledMouseEventArgs)e).Handled = true;