在跟踪栏ValueChanged上触发事件,但在代码中未触发 [英] Trigger event on trackbar ValueChanged, but not in code

查看:181
本文介绍了在跟踪栏ValueChanged上触发事件,但在代码中未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在不触发事件处理程序的情况下修改代码中跟踪栏的value属性.我希望仅在用户通过拖动滑块或使用键盘移动控件来更改控件时才触发事件.实现这一目标的最简单方法是什么?

I want to be able to modify the value property of a trackbar in code without triggering my event handler. I wish to trigger the event only when the control is changed by the user by dragging the slider or moving it with the keyboard. What's the simplest way of achieving this?

我有6个跟踪栏,我想根据更改的跟踪栏更改其中3个的值.问题在于,更改这些跟踪栏的值将触发其ValueChanged事件.

I have 6 trackbars and I want to change the value of 3 of them depending on which trackbar is changed. The issue is that changing the value of those trackbars will trigger their ValueChanged events.

推荐答案

执行此操作的一种方法是在修改代码中的值之前临时删除事件处理程序,然后再重新附加它们,尽管这似乎并不可行.太优雅了.

One way you can do this is to temporarily remove the event handlers before you modify the values in code, and then reattach them afterwards, although this doesn't seem too elegant.

或者,您可以创建自己的自定义TrackBar类,该类继承自TrackBar,并重写OnValueChanged()方法以执行所需的操作.

Alternatively you could create your own custom TrackBar class that inherits from TrackBar and override the OnValueChanged() method to do what you want.

如果您这样做,我可以想到的一个想法是在更改值之前设置SuspendChangedEvents属性,然后再对其进行重置,这将提供与删除/附加"处理程序技术类似的功能,但逻辑封装在其中TrackBar本身.

If you did this, an idea I can think of is to set a SuspendChangedEvents property before changing the value, and reset it afterwards, This would provide similar functionality to the 'remove/attach' handler technique but the logic is encapsulated within the TrackBar itself.

public class MyTrackBar : TrackBar
{
    public bool SuspendChangedEvent
    { get; set; }

    protected override void OnValueChanged(EventArgs e)
    {
        if (!SuspendChangedEvent) base.OnValueChanged(e);
    }
}

然后在您的代码中,您可以执行以下操作.

Then in your code you can do something like this.

// Suspend trackbar change events
myTrackBar1.SuspendChangedEvents = true;

// Change the value
myTrackBar1.Value = 50;  // ValueChanged event will not fire.

// Turn changed events back on
myTrackBar1.SuspendChangedEvents = false;

这篇关于在跟踪栏ValueChanged上触发事件,但在代码中未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆