Hi, can anyone advise how to make a button run code only while it is pressed? Basically I have found this code on the Net for changing the system volume, but you have to keep clicking on the buttons to change the volume. I want to be able to hold the UP/Down button down to continually increase/de-crease the volume.

Option Explicit On
Imports System.Runtime.InteropServices


Public Class Form1
Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
Private Const APPCOMMAND_VOLUME_Down As Integer = &H90000
Private Const WM_APPCOMMAND As Integer = &H319
Private bRunning As Boolean
<DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal iParam As Integer) As IntPtr
End Function

'Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
'End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_Down))
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
End Sub

Private Sub Button1_MouseDown(Button As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
bRunning = True
Call doLoopedStuff()
End Sub

Private Sub Button1_MouseUp(Button As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
bRunning = False
Call doLoopedStuff()
End Sub

Private Sub doLoopedStuff()
Do While (bRunning = True)
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
Loop
End Sub

End Class

The problem I have with this code is that once I have pressed the UP button it starts the loop but ignores the MouseUP part. Infact I have to stop debugging to break out of it.

How about this.

  • Put a timer on the form
  • Enable the timer on mouse down
  • Disable the timer on mouse up or max volume reached

The timer could be set to an interval so that the min to max volume can be traversed in (for example) five seconds. Each tick of the timer would increase the volume by a set amount. The up and down buttons could set the increment (or decrement) value so the same timer could be used for both up and down. I tried this out and I think it will do what you want. Here is an example.

Public Class Form1

    Private current As Integer = 
    Private increment As Integer = 0

    Private Sub btnUp_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles btnUp.MouseDown
        increment = 1
        Timer1.Enabled = True
    End Sub

    Private Sub btnUp_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles btnUp.MouseUp
        Timer1.Enabled = False
    End Sub

    Private Sub btnDown_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles btnDown.MouseDown
        increment = -1
        Timer1.Enabled = True
    End Sub

    Private Sub btnDown_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles btnDown.MouseUp
        Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick

        If increment > 0 Then
            If current = 20 Then
                Timer1.Enabled = False
                Exit Sub
            End If
        Else
            If current = 0 Then
                Timer1.Enabled = False
                Exit Sub
            End If
        End If

        current += increment
        Me.Text = current

    End Sub

End Class

The value of "current" will be written to the title bar.

Edited 4 Years Ago by Reverend Jim: added detail

Thanks for that but unless I'm doing something wrong, the program still doesn't break the loop. All buttons are unresponsive and the UP button stays in the down position.
Are there any working examples I can download so I can get my head around it as I'm new to this and i'm probably using the code you have given incorrectly.

Not to worry, I have figured it out. Thanks for the help.......

This question has already been answered. Start a new discussion instead.