【问题标题】:A Label in C# That has a long pressC# 中长按的标签
【发布时间】:2018-04-10 14:17:06
【问题描述】:

嗨,我的表单上有一个标签,我想在它执行显示消息框之类的功能之前按下 2.5 秒,我尝试使用鼠标向下和鼠标向上但没有成功,它没有等待我告诉它到。任何事情都会有所帮助。谢谢。

 private void Ltitelpress(object sender, EventArgs e)
    {
        lTitel.MouseDown+=delegate(object o, MouseEventArgs args) {  };
       Timer timer = new Timer();
        timer.Start();
        timer.Interval = 2500;
        if (timer.Interval == 2500)
        {
        lTitel.MouseUp+= delegate(object a, MouseEventArgs args)
        {
            timer.Stop();
            MessageBox.Show("Good Job");
        };

        }
    }

问题是我什至不知道这段代码是否接近准确度

【问题讨论】:

  • 发布您的代码,人们可以帮助修复它。
  • 我发布了它,我对编程很陌生,所以我不知道我是否离开了我的摇杆

标签: c# label long-press


【解决方案1】:

在类级别(表单级别)声明一个秒表,以便您可以从 2 个不同的事件过程中访问它。

private Stopwatch sw = new Stopwatch();
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            sw.Start();
        }
        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            sw.Stop();
            if (sw.ElapsedMilliseconds > 2500)
                MessageBox.Show("Mouse held down long enough.");
            else
                MessageBox.Show("Not long enough.");
        }

【讨论】:

  • 帮助很大
【解决方案2】:

只需将您的代码移动到LabelMouseUP 事件,它只会在您从标签上释放鼠标点击时触发

private void label_MouseUP()
///codes here

或者如果你想以艰难的方式做到这一点:)

private void label_MouseDown()
  var watch = System.Diagnostics.Stopwatch.StartNew();
  double elapsedMs = watch.ElapsedMilliseconds;

   if (elapsedMs > 2500)
    { 
     ////codes here
    }

【讨论】:

  • 问题是它总是在鼠标启动时触发,无论计时器如何
  • 我希望它只在我按住它时触发
  • 如果有帮助,请务必将答案标记为答案 :)
  • @zackraiyan 当然这有点不完整,因为它会启动计时器,它会在经过时为 0,然后 0 总是
  • 据我所知,在秒表运行时也可以查询ElapasedMilliseconds..看看at this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多