在轨迹栏 ValueChanged 上触发事件,但不在代码中触发

发布于 2024-09-15 03:39:41 字数 169 浏览 4 评论 0原文

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

我有 6 个轨迹栏,我想根据更改的轨迹栏来更改其中 3 个的值。问题是更改这些跟踪栏的值将触发其 ValueChanged 事件。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2024-09-22 03:39:41

您可以使用 Scroll 事件而不是 ValueChanged,此事件在用户拖动滑块或使用按键移动其值时触发,并且您对代码所做的更改不会触发它。

You could use the Scroll event instead of ValueChanged, this event only gets fired when the user drags the slider or uses the keys to move its value, and the changes you make from the code won't fire it.

南烟 2024-09-22 03:39:41

执行此操作的一种方法是在修改代码中的值之前暂时删除事件处理程序,然后重新附加它们,尽管这看起来不太优雅。

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

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

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

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

然后在你的代码中你可以做这样的事情。

// 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;

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.

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文