Keydown event

W

win

I'm convert a VB6 program to .Net platform.
My program use function key (F12) to close a form.
Now I found that the coding in the keydown event of control triggered, the
coding in the keydown of the form does not triggered.

Can anyone help me?
Thanks a lot.
 
A

Armin Zingler

win said:
I'm convert a VB6 program to .Net platform.
My program use function key (F12) to close a form.
Now I found that the coding in the keydown event of control
triggered, the coding in the keydown of the form does not triggered.


Set the Form's KeyPreview property to True. (like it was in VB6)


Armin
 
K

kimiraikkonen

I'm convert a VB6 program to .Net platform.
My program use function key (F12) to close a form.
Now I found that the coding in the keydown event of control triggered, the
coding in the keydown of the form does not triggered.

Can anyone help me?
Thanks a lot.

While your form active, that code must work:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F12 Then
Me.Close()
End If
End Sub
 
K

kimiraikkonen

While your form active, that code must work:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F12 Then
Me.Close()
End If
End Sub

Note: When there's nothing on form like buttons, listboxes or others,
this code works. But if there are some other controls placed on form,
i don't know the reason that it doesn't work, maybe focusing problem?
 
H

Herfried K. Wagner [MVP]

win said:
I'm convert a VB6 program to .Net platform.
My program use function key (F12) to close a form.
Now I found that the coding in the keydown event of control triggered, the
coding in the keydown of the form does not triggered.

Either set the form's 'KeyPreview' property to 'True' or add a mainmenu
component with a menu item which has the appropriate shortcut.
 
C

Cor Ligthert[MVP]

Win,

As forever about this question I ask another question, why the keydown and
not the keyup, that one gives a lot of information which keys have been
pressed.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top