By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Keypressed event of a form

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Himselff wrote:
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

--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #2

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

Thanks !

Him
"C-Services Holland b.v." <cs*@REMOVEcsh4u.nl> a écrit dans le message de
news: 3o********************@zeelandnet.nl...
Himselff wrote:
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

--
Rinze van Huizen
C-Services Holland b.v.

Nov 21 '05 #3

P: n/a
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

"Himselff" <flamontagne@no_spam.polyform.com> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #4

P: n/a
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 !
"Peter Proost" <pp*****@nospam.hotmail.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP09.phx.gbl...
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

"Himselff" <flamontagne@no_spam.polyform.com> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
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


Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
Himselff wrote:
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.

--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #7

P: n/a
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 !

"Cor Ligthert" <no************@planet.nl> a écrit dans le message de news:
Ob**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #8

P: n/a
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-Services Holland b.v." <cs*@REMOVEcsh4u.nl> a écrit dans le message de
news: V4********************@zeelandnet.nl...
Himselff wrote:
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.

--
Rinze van Huizen
C-Services Holland b.v.

Nov 21 '05 #9

P: n/a
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" <flamontagne@no_spam.polyform.com> a écrit dans le message de
news: eb****************@tk2msftngp13.phx.gbl...
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-Services Holland b.v." <cs*@REMOVEcsh4u.nl> a écrit dans le message de
news: V4********************@zeelandnet.nl...
Himselff wrote:
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.

--
Rinze van Huizen
C-Services Holland b.v.


Nov 21 '05 #10

P: n/a
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
Nov 21 '05 #11

P: n/a
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 !

"Nigel Armstrong" <Ni************@discussions.microsoft.com> a écrit dans le
message de news: 41**********************************@microsoft.com...
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

Nov 21 '05 #12

P: n/a

"Himselff" <flamontagne@no_spam.polyform.com> wrote
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
Nov 21 '05 #13

P: n/a

"Himselff" <flamontagne@no_spam.polyform.com> wrote
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
Nov 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.