Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to detect button press for 5 seconds continously and then open a new dialog window .
Posted

Hi rajeshlokayata,

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

C#
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);
        }
    }
}


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!
 
Share this answer
 
v2
Comments
explorerC 27-Jun-12 9:24am    
Im using a wpf Application
johannesnestler 29-Jun-12 5:50am    
Hi rajeshlokayata, Also in WPF the pattern is the same. If you don't want to use the System.Windows.Forms timer in WPF (you can! - just add reference to System.Windows.Forms dll...) Otherwise use the DispatchTimer (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx) which does the same for WPF. If you use any asynchron (to GUI thread) timer you have to marshall/dispatch the calls to the GUI elements - seems this is your problem - maybe read a little on thread marshalling or threading..
explorerC 9-Jul-12 22:24pm    
Hi johannesnestler,
Thanks for the update .
Deepak.Kamarsu 1-May-13 4:33am    
In a touch screen machine, On windows XP click is mouse down and mouse up event, however if you use your finger instead of the mouse, you might get a drag motion instead of the click, as when you press mouse down you finger might slightly move to the side from the initial position and you will get a drag instead of click.

At this scenario, can you please let me know the WPF button events for the same example(A window after 5 seconds of button press)
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.
 
Share this answer
 
Comments
explorerC 21-Jun-12 5:28am    
This requirement is for a Touch screen application .I tried with mousedown and mouseup events with a timer that triggers after 5 seconds it is not working.

Im using a wpf application .

Im getting the exception the calling thread cannot access this object because a different thread owns it.


System.Timers.Timer objTim = new System.Timers.Timer(5000);
private void Button_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// TODO: Add event handler implementation here.


objTim.Start();
}




bool tempSignal = true;
private void Button_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
tempSignal = false;
// objTim.Stop();

}


private void objTim_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (tempSignal)
{
genSetSubWinObj.Show();
}


}
johannesnestler 21-Jun-12 5:44am    
Because System.Timer.Timer Elapsed event doesn't fire on the Gui thread, if I remember right. Just marshall/dispatch to GUI thread or use another timer.
John Orendt 21-Jun-12 15:05pm    
Yes, use System.Windows.Forms.Timer
explorerC 27-Jun-12 9:15am    
Im using WPF .......It is not avaible there......Is there any alternative
explorerC 29-Jun-12 4:44am    
Hi Guys,
It is working on my system.But got a problem working it on touch screen enabled screen.
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
C#
if( b5SecsElapsed )
{
  // open a new dialog window . 
}
 
Share this answer
 
Comments
lukeer 21-Jun-12 3:51am    
<muttering>Grmph! 2 fast 4 me.</muttering>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900