Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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:

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:

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

Share

About the Author

Daniel Presman
Web Developer
Brazil Brazil
No Biography provided

Comments and Discussions

 
GeneralTimer function Pinmemberjady8430-Nov-06 1:45 
GeneralRe: Timer function PinmemberDaniel Presman19-Dec-06 3:57 
GeneralFile name using timer Pinmemberdunecherng21-Feb-06 21:36 
GeneralRe: File name using timer PinmemberDaniel Presman22-Feb-06 6:51 
GeneralRe: File name using timer Pinmemberdunecherng23-Feb-06 3:25 
GeneralHere's the code... PinmemberLaurence O'Halloran9-May-05 11:38 
GeneralRe: Here's the code... PinmemberJeff Firestone6-Feb-06 6:45 
GeneralUPDATE PinmemberLaurence O'Halloran9-May-05 11:34 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 24 Oct 2003
Article Copyright 2003 by Daniel Presman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid