Keypressed event of a form

H

Himselff

Hi guys,

Im new to dot net and wondering how i could remake the same way i was in
vb6, capture all keypress whitin a form,

per exemple i was on the form looking for some keypress

privete sub form_keypressed(keyascii as integer)
if keyascii = 13 then 'for the enter key
call button_click
end if
end sub

what i got so far in vb .net is :

Private Sub FrmMain_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

If e.KeyChar = "P" Or e.KeyChar = "p" Then

e.Handled = True

Call BtmProduit_Click(sender, e)

End If

end sub

which is not working and dosent fire any event at all, so after looking the
forum and internet, ive found nothing that was working,

Thanks a lot in advance !

Him
 
H

Himselff

Ive Read about the keypreview but i cannot find anything , is that a form
attribute ? like form.keypreview ?

Thanks !

Him
 
H

Himselff

Thats exactly what ive read but where u get the keypreview from ?

when i write form1.keypreview it says that i have an error, like :
keypreview is not a member of form1,

any clue ?

Thnaks !
 
C

C-Services Holland b.v.

Himselff said:
Hi guys,

Im new to dot net and wondering how i could remake the same way i was in
vb6, capture all keypress whitin a form,

per exemple i was on the form looking for some keypress

privete sub form_keypressed(keyascii as integer)
if keyascii = 13 then 'for the enter key
call button_click
end if
end sub

what i got so far in vb .net is :

Private Sub FrmMain_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

If e.KeyChar = "P" Or e.KeyChar = "p" Then

e.Handled = True

Call BtmProduit_Click(sender, e)

End If

end sub

which is not working and dosent fire any event at all, so after looking the
forum and internet, ive found nothing that was working,

Thanks a lot in advance !

Him

Did you set the keypreview setting of the form to true?
Also I'd change your if to something like:

If UCase(e.KeyChar)="P" then
 
P

Peter Proost

set the form keypreview property to true to capture all keypresses on form,
even if they're inside a textbox, use keydown to check. fe

if e.keycode = keys.enter then
'do stuff
end if

hth Peter
 
H

Himselff

That part dont realy apply to me cause i want to emulate button click from a
keypress event so all event are handle from form event handler !

I understand the point by the way and i take all in note in case i need it
someday ,

thanks for the info !
 
C

Cor Ligthert

Himselff,

In VBNet you can make an array of the controls that you want to use and with
that tell exactly controls should have that event. Keypress is an event from
control so I think you cannot come in much problems with that. (I thought
that there are controls where that event is hidden so do not think that it
directly works everywhere).

All is typed in this messages not tested so watch errors or typos.

You can make that array like this
\\\
Dim mykeypressctrs as control() = new control() {textbox1, richtextbox1,
textbox2, etc}
///
And than something as this to add the handlers to that.
\\\
For Each ctr As Control In mykeypressctrs
AddHandler ctr.keypress, AddressOf FrmMain_KeyPress
Next
///

I hope this helps?

Cor
 
H

Himselff

When u say newby remember himselff =)

Ive been trough all option of the form in designer and i dont find anything
that look like keypreview, mabe im missing something here ?

Thanks !
 
C

C-Services Holland b.v.

Himselff said:
Ive Read about the keypreview but i cannot find anything , is that a form
attribute ? like form.keypreview ?

Thanks !

Him

If you select your form in the designer, one of the properties is
keypreview.
 
H

Himselff

By the way i just forget one detail, im developing for a pocket PC windows
CE .net mabe its the reason i dont have the keypreview on my form ?

Him
Himselff said:
When u say newby remember himselff =)

Ive been trough all option of the form in designer and i dont find
anything that look like keypreview, mabe im missing something here ?

Thanks !
 
G

Guest

If you can't see the KeyPreview property, it could be that you have a control
with the focus. Click on the form background to make sure that you can see
the property!

HTH

Nigel
 
H

Himselff

I realy dont have that option, no control got the focus, even when i type in
my code frmmain.something it dont show up, as i said previously , is there
any chance that the fact that im developing a project for pocket pc dont
allow any keypress event handler ?

Thanks !
 
L

Larry Serflaten

Himselff said:
By the way i just forget one detail, im developing for a pocket PC windows
CE .net mabe its the reason i dont have the keypreview on my form ?

Yes, that is the reason. The PPC has an OnScreen Keyboard to allow
users to enter text into textboxes. Its not the same model as the Windows
Forms....

LFS
 

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