keydown, but for second press?

wojtek1150

Member
Joined
Sep 14, 2011
Messages
5
Programming Experience
1-3
How to make an event for keydown, but for second press?

Yes, i know how to make event, eg.:

VB.NET:
If e.KeyCode = Windows.Forms.Keys.Escape Then
            Me.Close()
        End If
But i want code for second press. For example...:

First use button space: open form1
Second use button space: open form2


Any sugestions?
 
Last edited by a moderator:
Use a variable to keep track of state, for example a number or a boolean. You can declare it Static locally, or declare it outside the method as a private class variable.
 
VB.NET:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Form's KeyPreview property must be set to True
        Static toggle As Boolean
        toggle = Not toggle
        If e.KeyCode = Windows.Forms.Keys.Escape Then
            If toggle = True Then
                MessageBox.Show("Toggle is True", "First")
            ElseIf toggle = False Then
                MessageBox.Show("Toggle is False", "Second")
            End If
        End If
    End Sub
 
Back
Top