如何检测按钮按下5秒钟,然后打开一个对话框窗口 [英] How to detect button press for 5 seconds and then open a dialog window

查看:111
本文介绍了如何检测按钮按下5秒钟,然后打开一个对话框窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何连续检测按钮按下5秒钟,然后打开一个新的对话框窗口.

How to detect button press for 5 seconds continously and then open a new dialog window .

推荐答案

对于WinForm类Button

这是解决方案的概述

您将处理事件KeyDown和KeyUp

还可以使用Timer类.

在事件KeyDown上启动计时器.

如果发生TimerTime5Secs
设置布尔b5SecsElapsed = true;

如果发生KeyUp
For WinForm class Button

Here is an outline of a solution

you would process events KeyDown and KeyUp

Also use class Timer.

Start Timer on event KeyDown.

In event TimerEvent5Secs
set bool b5SecsElapsed=true;

In event KeyUp
if( b5SecsElapsed )
{
  // open a new dialog window . 
}


订阅Control.KeyDownControl.KeyUp事件.在KeyDown上,启动5秒钟Timer.在计时器触发时,打开新窗口.

在KeyUp上,停止计时器,以使其不会触发.
Subscribe to Control.KeyDown and Control.KeyUp events. On KeyDown, start a 5 second Timer. On timer firing, open the new window.

On KeyUp, stop the timer so that it won''t fire.


您好rajeshlokayata,

这是一个可运行的示例-只需将其复制到新的WindowsForms项目中,并用以下代码替换Program.cs内容:

Hi rajeshlokayata,

Here is a runable example - just copy to a new WindowsForms project and replace Program.cs content with the following code:

using System;
using System.Windows.Forms;

namespace OpenFormAfter5Seconds
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // Create a test form with a button
            Form form = new Form();
            Button button = new Button();
            button.Text = "Press for 5 s";
            button.Dock = DockStyle.Fill;
            form.Controls.Add(button);

            // Create a timer for meassuring the duration
            Timer timer = new Timer();
            timer.Interval = 5000; // 5 Seconds

            // Process Button MouseDown 
            button.MouseDown += delegate(object sender, MouseEventArgs mea)
            {
                // Start the timer 
                timer.Start();
            };

            // Process Button MouseUp 
            button.MouseUp += delegate(object sender, MouseEventArgs mea)
            {
                // Stop the timer 
                timer.Stop();
            };

            // If Timer.Tick fires (after timer intervall has elapsed)
            timer.Tick += delegate(object sender, EventArgs e)
            {
                // ... stop the timer - it shouldn't fire again
                timer.Stop();

                // ... Open the new dialog - let's use a MessageBox here for demonstration
                MessageBox.Show(form, "You Pressed the Button for 5 Seconds!", "Dialog");
            };

            // run the example
            Application.Run(form);
        }
    }
}



因此,实际上,您可以在Forms设计器中设计表单,将事件处理程序附加到您的按钮(MouseDown/Up或KeyDown/Up),在ButtonDown上启动计时器,在ButtonUp上停止计时器(对于用户没有按住按钮的情况)按钮5秒钟),如果计时器间隔已过,请显示对话框!



So in reality you would design your form in the Forms designer, attach eventhandlers to your button (MouseDown/Up or KeyDown/Up), Start the timer on ButtonDown, Stop it on ButtonUp (for the cases the user didn''t hold the button for 5 Seconds), and if the timer interval has elapsed show your dialog!


这篇关于如何检测按钮按下5秒钟,然后打开一个对话框窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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