Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / Visual Basic
Article

A Simple Auto Repeat Button on VB.NET

Rate me:
Please Sign up or sign in to vote.
2.64/5 (18 votes)
23 Oct 20032 min read 108.9K   1.4K   13   9
Auto repeat button in VB.NET based on timer interval

Sample Image - RepeatButton.jpg

Introduction

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:

VB
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).

Code

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:

VB
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
         Get
               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 
          OnClick(EventArgs.Empty)
     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

Conclusion

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

License

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


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 1114350110-Oct-14 1:26
Member 1114350110-Oct-14 1:26 
GeneralTimer function Pin
jady8430-Nov-06 1:45
jady8430-Nov-06 1:45 
GeneralRe: Timer function Pin
Daniel Presman19-Dec-06 3:57
Daniel Presman19-Dec-06 3:57 
GeneralFile name using timer Pin
dunecherng21-Feb-06 21:36
dunecherng21-Feb-06 21:36 
GeneralRe: File name using timer Pin
Daniel Presman22-Feb-06 6:51
Daniel Presman22-Feb-06 6:51 
GeneralRe: File name using timer Pin
dunecherng23-Feb-06 3:25
dunecherng23-Feb-06 3:25 
GeneralHere's the code... Pin
Member 13500489-May-05 11:38
Member 13500489-May-05 11:38 
Some very simple changes...

In the class file...


Public Class RepeatButton
Inherits Button
Public Sub New()
AddHandler timer.Tick, AddressOf OnTimer
Timer.Enabled = False
Timer.Interval = 100
End Sub
Public Timer As New Timer
Public Property Interval() As Integer
Get
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
OnClick(EventArgs.Empty)
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


On the form... only need to access the click event...commented out the laod and timer tick code


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'cmdIncrease.Timer = TimerInc
'cmdDecrease.Timer = TimerDec
'cmdIncrease.Interval = 200
'cmdDecrease.Interval = 200
End Sub

'Private Sub TimerInc_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerInc.Tick
' If (txtVolume.Text < 98) Then
' txtVolume.Text += 1
' End If
'End Sub

'Private Sub TimerDec_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDec.Tick
' If (txtVolume.Text > 0) Then
' txtVolume.Text -= 1
' End If
'End Sub

Private Sub cmdDecrease_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecrease.Click
If (txtVolume.Text > 0) Then
txtVolume.Text -= 1
End If
End Sub

Private Sub cmdIncrease_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIncrease.Click
If (txtVolume.Text < 98) Then
txtVolume.Text += 1
End If
End Sub
GeneralRe: Here's the code... Pin
Jeff Firestone6-Feb-06 6:45
Jeff Firestone6-Feb-06 6:45 
GeneralUPDATE Pin
Member 13500489-May-05 11:34
Member 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.