Click here to Skip to main content
14,151,118 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 23 Oct 2003

A Simple Auto Repeat Button on VB.NET

, 23 Oct 2003
Rate this:
Please Sign up or sign in to vote.
Auto repeat button in VB.NET based on timer interval

Sample Image - RepeatButton.jpg


I wanted to use in my application a simple button to increase and decrease the volume of the music. I wanted the volume to keep increasing while the user holds the button. I found out that it is not so simple, since both the click event of the button and the MouseButtons property can't do that.

I searched the Internet and I found C# control samples for a Auto Repeat Button, but I didn't find any VB.NET examples. So I decided to create a RepeatButton that inherits from button and can keep doing something while the user holds the button.

Technical information

The control inherits from Button and has a Timer in it. When user clicks on the button (on the MouseDown event) we start the timer and when he leaves the mouse (on MouseUp event) we stop the timer.

The timer is actually what keeps the function that is going to executed while user holds the button. Any timer in the project can be associated to the RepeatButton. The RepeatButton has also an Interval property which can be set to be the interval of the timer associated to the RepeatButton. For example:

Dim cmdIncrease As New RepeatButton 'create new RepeatButton
cmdIncrease.Timer = TimerInc 
'Associate TimerInc to the Button 
'(supose timerInc is a Timer in the project)
cmdIncrease.Interval = 200  'Sets the interval for the timer

The code above sets the RepeatButton cmdIncrease to execute the TimerInc.Tick function every 200ms while the user holds the button. When user leaves the button, the timer stops (at MouseUp event).


The code for the RepeatButton is a single class. It can be downloaded as the source. It is actually a DLL that can be downloaded and executed and then you can add the RepeatButton control to your application simply by adding a reference to this DLL or adding it to your toolbox in design mode.

There is also a complete sample project that can be downloaded and executed.

This is the code for the RepeatButton class:

Public Class RepeatButton
               Inherits System.Windows.Forms.Button

     Public Sub New()
          AddHandler timer.Tick, AddressOf OnTimer
          timer.Enabled = False
     End Sub

     Public Timer As New timer
     Public Property Interval() As Integer
               Return timer.Interval
         End Get

         Set(ByVal Value As Integer)
               timer.Interval = Value
         End Set
    End Property

     Private Sub OnTimer(ByVal sender As Object, ByVal e As EventArgs)
          'fire off a click on each timer tick 
     End Sub

     Private Sub RepeatButton_MouseDown(ByVal sender As Object, ByVal e As  _
     System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
          'turn on the timer 
          timer.Enabled = True
     End Sub

     Private Sub RepeatButton_MouseUp(ByVal sender As Object, ByVal e As _
     System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
          ' turn off the timer 
          timer.Enabled = False
     End Sub

End Class


I hope you find this code useful. It's very easy to use.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Daniel Presman
Web Developer
Brazil Brazil
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Member 1114350110-Oct-14 1:26
memberMember 1114350110-Oct-14 1:26 
GeneralTimer function Pin
jady8430-Nov-06 1:45
memberjady8430-Nov-06 1:45 
GeneralRe: Timer function Pin
Daniel Presman19-Dec-06 3:57
memberDaniel Presman19-Dec-06 3:57 
GeneralFile name using timer Pin
dunecherng21-Feb-06 21:36
memberdunecherng21-Feb-06 21:36 
GeneralRe: File name using timer Pin
Daniel Presman22-Feb-06 6:51
memberDaniel Presman22-Feb-06 6:51 
GeneralRe: File name using timer Pin
dunecherng23-Feb-06 3:25
memberdunecherng23-Feb-06 3:25 
GeneralHere's the code... Pin
Member 13500489-May-05 11:38
memberMember 13500489-May-05 11:38 
GeneralRe: Here's the code... Pin
Jeff Firestone6-Feb-06 6:45
memberJeff Firestone6-Feb-06 6:45 
GeneralUPDATE Pin
Member 13500489-May-05 11:34
memberMember 13500489-May-05 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190518.1 | Last Updated 24 Oct 2003
Article Copyright 2003 by Daniel Presman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid