Question : disabling key stroke events vb.net?

Hello experts I have an event that fires when certain keys are pressed and a procedure is executed.  The problem is that if they hold down the key the event will fire rapidly.  I tried putting in a thread.sleep, but that doesnt work, it will just queue up a hold bunch then fire them all at once, when it exits the sleep.  So basically how can i disable the key stroke event while the procedure is firing?

  Private Sub txtSelection_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
         Select Case e.KeyCode
            Case Keys.F1
                Me.Close()
            Case Keys.D1 'when 1 is pressed thats when I want it to disable then reenable after it completes
                    getlabel.ForeColor = Color.Green
                    addnewlabel(x, d)
    System.Threading.Thread.Sleep(1000)
               
          Case Keys.D3
                MessageBox.Show("Do you wish to abort")
                getdt.Clear()
                Dim frm As New Form2
                Me.Hide()
                frm.ShowDialog()
                Me.Close()
            Case Keys.F10
                MessageBox.Show("Do you wish to quit")
                getdt.Clear()
                Dim frm As New Form1
                Me.Hide()
                frm.ShowDialog()
                Me.Close()
        End Select
        e.Handled = True
    End Sub

Answer : disabling key stroke events vb.net?

If you don't want the "event" to "fire" faster than a certain interval, then track the last Date/Time it was run so you can do a comparison...

    Private Sub txtSelection_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSelection.KeyUp
        Select Case e.KeyCode
            Case Keys.F1
                ' ...

            Case Keys.D1
                Static lastFired As DateTime = DateTime.MinValue
                Dim curTime As DateTime = DateTime.Now
                Dim ts As TimeSpan = curTime.Subtract(lastFired)
                If ts.TotalMilliseconds > 1000 Then ' <--- adjust to your liking...
                    lastFired = curTime

                    ' do something in here...
                    getlabel.ForeColor = Color.Green
                    addnewlabel(x, d)
                End If

            Case Keys.D3
                ' ...

            Case Keys.F10
                ' ...

        End Select

    End Sub
Random Solutions  
 
programming4us programming4us