Live Chat Icon For mobile
Live Chat Icon

How do I make a Button to trigger a Click event at specific time intervals when the mouse is down like a ScrollBar button?

Platform: WPF| Category: Buttons

This can be done with the following piece of code.

[C#]
 
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 the timer
        timer1 = new Timer();
        timer1.Tick += new EventHandler( OnTimer );
        timer1.Enabled = false;
      }
      return timer1;
    }
  }

  protected override void OnMouseDown( MouseEventArgs me )
  {
    base.OnMouseDown( me );
    Timer.Enabled = true; // turn on the timer
  }

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

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

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.