如何使用鼠标按住和释放来检查TrackBar滑动 [英] How to check for TrackBar sliding with mouse hold and release

查看:127
本文介绍了如何使用鼠标按住和释放来检查TrackBar滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WinForms程序中有一个跟踪栏,通过移动它,可以刷新一个庞大且耗时的方法.看一下这段代码:

I have a trackbar in my WinForms program which by moving it a huge and time consuming method will be refreshed. Have a look at this code:

trackBar_ValueChanged(object sender, EventArgs e)
{
     this.RefreshData();
}

此跟踪栏有20个步骤.如果在我的情况下,用户抓住轨迹栏的滑块并将其从20拉到0,则"RefreshData"将执行20次,尽管它在结束值为0的"RefreshData"过程时会显示正确的数据,我想执行类似的操作仅在释放trackBar滑块后才调用"RefreshData",因此它不会处理到轨迹栏上释放点的所有步骤.

This track bar has 20 steps. If user grab the trackbar's slider and pull it from 20 to 0 in my case, 'RefreshData' will be executed 20 times although it shows correct data when it ends the process that is 'RefreshData' with value 0, I want to do something like it only calls the 'RefreshData' when the trackBar slider has been released so it wont process all the steps to the releasing point on the track bar.

任何帮助和技巧达到这一目标的人都将被奉为! 谢谢.

Any help and tips to achieve this would be appericiated! Thanks.

推荐答案

我之前做这种事情的方式是使用计时器.每次TrackBar的值更改时,请重置计时器,以使其在500毫秒内触发(或任何适合您的时间).如果用户在计时器启动之前更改了该值,它将再次重置,这意味着即使多次更改,计时器也只会启动一次.

The way that I've done this sort of thing before is to use a timer. Every time the value of the TrackBar changes, reset the timer so that it fires in 500ms (or whatever is appropriate for you). If the user changes the value before the timer fires, it will be reset again, meaning that even if it changes multiple times, the timer will only fire once.

这里唯一需要注意的是计时器将在另一个线程上触发,并且您将无法从该线程更新UI,因此您必须调用UI线程才能在那里进行更改.就是说,但是,如果您可以将大部分昂贵的工作移至该后台线程,则可以使UI响应时间更长.

The only thing to watch here is that the timer will fire on a different thread and you won't be able to update the UI from that thread, so you'll have to invoke back onto the UI thread to make changes there. That said, however, if you can move the majority of your expensive work onto this background thread, you'll keep the UI responsive for longer.

这里有一些示例代码,应该使您对我的意思有所了解.

Here's some sample code that should give you some idea of what I mean.

public partial class Form1 : Form
{
    private int updateCount;
    private System.Threading.Timer timer;

    public Form1()
    {
        this.InitializeComponent();
        this.timer = new System.Threading.Timer(this.UpdateValue);
    }

    private void UpdateValue(object state)
    {
        // Prevent the user from changing the value while we're doing
        // our expensive operation
        this.Invoke(new MethodInvoker(() => this.trackBar1.Enabled = false));

        // Do the expensive updates - this is still on a background thread
        this.updateCount++;

        this.Invoke(new MethodInvoker(this.UpdateUI));
    }

    private void UpdateUI()
    {
        this.label1.Text = this.updateCount.ToString();

        // Re-enable the track bar again
        this.trackBar1.Enabled = true;
    }

    private void trackBar1_ValueChanged(object sender, EventArgs e)
    {
        this.timer.Change(TimeSpan.FromMilliseconds(500), new TimeSpan(-1));
    }
}

这是一个使用获胜表格计时器进行计时的解决方案.此处的区别在于,您将在计算运行时将UI锁定;根据您的情况,这可能会也可能不会.

Here's a solution that uses a win forms timer to do the timings. The difference here is that you'll lock the UI up while the calculation is running; this may or may not be ok in your situation.

public partial class Form1 : Form
{
    private int updateCount;
    private Timer timer;

    public Form1()
    {
        this.InitializeComponent();

        this.timer = new Timer();
        this.timer.Interval = 500;
        this.timer.Tick += this.Timer_Tick;
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        this.timer.Stop();

        this.updateCount++;
        this.label1.Text = this.updateCount.ToString();
    }

    private void trackBar1_ValueChanged(object sender, EventArgs e)
    {
        this.timer.Stop();
        this.timer.Start();
    }
}

这篇关于如何使用鼠标按住和释放来检查TrackBar滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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