hi,

i want to ask about keyevent when keypress on form load in a function.
what i want is when i click "F2" then the key of "F12" and a button is enabled=false.


Anyone, please help me..:?:
thank you.

Set KeyPreview on your form properties as True

Then Try this :

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.F2 Then
	Button1.Enabled = False
    End If
End Sub

Edited 5 Years Ago by Jx_Man: typo

Comments
I also want to know how the F2 works.

What is the F12 is doing? Would you please post that piece of code?

I guess what you need is something like:

If ThatButton.Enabled then
‘do the job
End If

Edited 5 Years Ago by PdotWang: n/a

thank you..
but for enabled=false the other key such as F12?
How the code?

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True '// keep focus of keys on Form.
    End Sub
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F2 Then If Button1.Enabled = True Then Button1.Enabled = False '// Disable.
        If e.KeyCode = Keys.F12 Then If Button1.Enabled = False Then Button1.Enabled = True '// Enable.
    End Sub
End Class

Edited 5 Years Ago by codeorder: n/a

thank you.
i mean how to disable the user to press F2 after they press F12?

Private bAllowF2KeyPress As Boolean = True

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If bAllowF2KeyPress = True Then
            If e.KeyCode = Keys.F2 Then If Button1.Enabled = True Then Button1.Enabled = False '// Disable.
        End If
        If e.KeyCode = Keys.F12 Then
            If Button1.Enabled = False Then Button1.Enabled = True '// Enable.
            bAllowF2KeyPress = False '// set to False to not allow the F2 key to disable Button.
        End If
    End Sub

I do not understood him until now (I think I do, maybe wrong again.)

He said, “when I click "F2" then the key of "F12" and a button is enabled=false”

My understanding is:

If the user press F2 and then (within 100 ms, for example) press F12 continuously, then disable the button.

Please confirm me, sarifah.

If I were to do it, I need a class scope variable bF2Clicked (Boolean). I may need a timer also.

bF2Clicked = False at Form Load
bF2Clicked = True at Form Load, and timer start.
bF2Clicked = False at timer stop.
Button1.Enable = False if bF2Clicked = True And Key = F12.

sorry if my question misunderstanding you.
what i want is the user can't click a button and press the F12 key after the user press F2 key.

thank.it is solved.

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