Live Chat Icon For mobile
Live Chat Icon

How can I get button to fire a click event at specific time intervals while the mouse is down like a scrollbar button?

Platform: WinForms| Category: Button

Shawn Burke posted the following solution on the microsoft.public.dotnet.framework.windowsforms newsgroup.

public class RepeatButton : Button 
{
	private Timer timer1;

	public int Interval 
	{
		get 
		{
			return Timer.Interval;
		}
		set
		{
			Timer.Interval = value;
		}
	}

	private Timer Timer 
	{
		get 
		{
			if (timer1 == null)
			{
                			// create and setup our timer.
               				 timer1 = new Timer();
                			timer1.Tick += new EventHandler(OnTimer);
                			timer1.Enabled = false;
			}
			return timer1;
		}
	}

	protected override void OnMouseDown(MouseEventArgs me) 
	{
		base.OnMouseDown(me);

        		// turn on the timer
        		Timer.Enabled = true;
    	}

	protected override void OnMouseUp(MouseEventArgs me) 
	{
		// turn off the timer
         		Timer.Enabled = false;
         		base.OnMouseUp(me);
	}

	private void OnTimer(object sender, EventArgs e) 
	{
            		// fire off a click on each timer tick
           		 //
           		 OnClick(EventArgs.Empty);
   	 }
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.