Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
1.57/5 (4 votes)
See more:
I made this vb.net program where i loaded all my songs from a particular folder in a listbox. I have two buttons- button1 is for loading the songs and button2 is what i intended for is simulating the down arrow key, so that whenever i click on button2, i go to the next song. But here is the problem, i used a timer to simulate the down arrow key and whenever i click on the button2 , the timer starts and the down arrow key keeps going continuously without stopping. This is not what i want, my goal is that the when the click on button2, it should just go 1 step and not keep going continuously. I tried using loops to stop that but nothing worked. Any idea how i can solve this issue.
Heres my code-
VB
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


        Timer1.Enabled = True

 End Sub

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

SendKeys.Send("{DOWN}")

End Sub
Posted
Updated 14-Sep-14 3:51am
v2
Comments
Wombaticus 14-Sep-14 9:05am    
Add
Timer1.Enabled = False
in the Timer!_Tick event. But what's the point of the Timer? WHy not just put your Sendkeys in the Button click event?
Sergey Alexandrovich Kryukov 14-Sep-14 9:11am    
There is no point of timer, and probably there is no a point of SendKeys. Please explain what are you trying to achieve and why.
—SA
Sinisa Hajnal 15-Sep-14 3:12am    
Just select next index in whatever list you're using for the song list. No need to call SendKeys or any other "magic function". Timers you can disable, but why would you even have them!?

1 solution

You can add this as a solution, as I have tested this and its working fine. Hope it helps.

VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'When formloads
    AddHandler eTB.KeyDown, AddressOf eTB_Ev 'Add a handler for the textbox we created in code.
    Me.Controls.Add(eTB) 'Now add the control
End Sub

Dim eWin_KeyDown As Integer = &H100 'Declared for API
Dim eWin_KeyUp As Integer = &H101 'Same ^

Public Declare Function eAPISMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
'The function above will be used to execute the send command.

Public WithEvents eTB As New TextBox 'Create an instance of a textbox.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    eTB.Focus() 'Give the TextBox Focus.
    eAPISMessage(eTB.Handle.ToInt32, eWin_KeyDown, eWin_KeyUp, CStr(0)) 'Using our API call we can send the message of keydown to the keydown event
End Sub

Private Sub eTB_Ev(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    'Dont add a handler because the API function is calling this sub and passing the key press. (Adding Handles eTB.KeyPress will only cause you to receive it twice.)
    MsgBox("Keydown pressed from Command3 button.") 'Remove this once you are happy it works.
    Try 'Use a try block to catch any index exception errors which you might have.

        'I also don't check for a key press for a specific key press because its passed by the API function.

        If ListBox1.SelectedIndex = 0 Then 'We will check for a zer index whcih would cause an exception.
            Exit Sub 'Exit the sub if there are no items to index
        Else
            Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1 'Select next Item.
        End If
    Catch ex As Exception
        'You can handle your error here or ignore it. It's safe to ignore this one. ;)
    End Try
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    'When the index is changed, this will fire.
    'Put your code here to play next song.
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900