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

How do I make the enter key act like a button click?

P: n/a
SJ
Hope someone can help me out there I'm struggling with a particular
problem...

I have a form with many tab pages. On one tab page I've got a button
which when clicked with a mouse adds items into a datagrid. When a
user presses the enter key on this button I want the mouse click event
to be activated but instead the tab page's validating event is called.

I understand that this behaviour is by partly by design (Accept button
etc) but I have not set an accept button for my form (because of the
many tab pages there is more than one button so an accept or a cancel
button is not viable).

In short - for my users who prefer the keyboard to the mouse - I want
an enter key press on a button to act like a button click using the
mouse.
How do I do this?

(Note as a part attempt to fix, I've tried setting causesvalidation
to false for my button to prevent it from calling the tab pages
validating event and this did not work - the validating event was
still triggered)

many thanks
Suzanne
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
You really dont want to do this. I would suggest that you create a sub which
has the functionalty you want and simply call it from the button, if you
want to call it from a mouse click for a given control as well you can do
that also.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"SJ" <su****************@yahoo.co.uk> wrote in message
news:3d*************************@posting.google.co m...
Hope someone can help me out there I'm struggling with a particular
problem...

I have a form with many tab pages. On one tab page I've got a button
which when clicked with a mouse adds items into a datagrid. When a
user presses the enter key on this button I want the mouse click event
to be activated but instead the tab page's validating event is called.

I understand that this behaviour is by partly by design (Accept button
etc) but I have not set an accept button for my form (because of the
many tab pages there is more than one button so an accept or a cancel
button is not viable).

In short - for my users who prefer the keyboard to the mouse - I want
an enter key press on a button to act like a button click using the
mouse.
How do I do this?

(Note as a part attempt to fix, I've tried setting causesvalidation
to false for my button to prevent it from calling the tab pages
validating event and this did not work - the validating event was
still triggered)

many thanks
Suzanne

Nov 21 '05 #2

P: n/a
Hilarious!
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))

Nov 21 '05 #3

P: n/a
Rather!

"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
Hilarious!
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))


Nov 21 '05 #4

P: n/a
SJ
Hi OHM

firstly thanks for replying.
But I'm not quite sure that you've answered my question. I am trying
to call the same sub if the user either clicks the button or presses
the enter key when focused on the button ( & I'm talking about the
same button here). However when the enter key is pressesed on the
button instead of the sub I want being called the tabpages validating
event is called. At the very least I want to prevent this from
happening. I only want the tabpages validating event to be called when
the user has finshed entering information & is attempting to leave the
tabpage.

My button is used to enter information, not validate it, I validate
when all info is entered and the user leaves the tabpage.

If it is not possible to get the enter key (When focused on the
button) to call the same sub as the click event, is it possible to
prevent it from triggering the tabpage's validating event?

Thanks
Suzanne


"One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> wrote in message news:<#2**************@TK2MSFTNGP09.phx.gbl>...
You really dont want to do this. I would suggest that you create a sub which
has the functionalty you want and simply call it from the button, if you
want to call it from a mouse click for a given control as well you can do
that also.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--

Nov 21 '05 #5

P: n/a
Well, at least it keeps the auto spammers at bay !

;-D

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
Hilarious!
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))


Nov 21 '05 #6

P: n/a
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ButtonDualHandler()
End Sub

Private Sub Button1_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

'Determines if return is pressed and then calls
'Our button handler if so
If e.KeyChar = Convert.ToChar(Keys.Return) Then
ButtonDualHandler()
End If

'prevents the normal handling of this event
e.Handled = True

End Sub

Private Sub ButtonDualHandler()
MsgBox("I got pressed or clicked")
End Sub

Private Sub Form1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
MsgBox("Validating")
End Sub

Private Sub Form1_Validated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Validated
MsgBox("Validated")
End Sub

HTH
--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"SJ" <su****************@yahoo.co.uk> wrote in message
news:3d**************************@posting.google.c om...
Hi OHM

firstly thanks for replying.
But I'm not quite sure that you've answered my question. I am trying
to call the same sub if the user either clicks the button or presses
the enter key when focused on the button ( & I'm talking about the
same button here). However when the enter key is pressesed on the
button instead of the sub I want being called the tabpages validating
event is called. At the very least I want to prevent this from
happening. I only want the tabpages validating event to be called when
the user has finshed entering information & is attempting to leave the
tabpage.

My button is used to enter information, not validate it, I validate
when all info is entered and the user leaves the tabpage.

If it is not possible to get the enter key (When focused on the
button) to call the same sub as the click event, is it possible to
prevent it from triggering the tabpage's validating event?

Thanks
Suzanne


"One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> wrote in
message news:<#2**************@TK2MSFTNGP09.phx.gbl>...
You really dont want to do this. I would suggest that you create a sub
which
has the functionalty you want and simply call it from the button, if you
want to call it from a mouse click for a given control as well you can do
that also.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--

Nov 21 '05 #7

P: n/a
SJ
hello again,

the below works just fine on a plain form but try it when the controls
are located on a tabpage on the form and you will see that pressing
the enter key on the button calls the tabpages validating event before
the **click** eventhandler . [Interestingly pressing the space bar
when focused on the button works as desired and just calls the
**keypress** event handler & not the tabpages validating event]

At this stage I'm happy just to tell my keyboard-friendly users to use
the space bar when focused on the button to get the same functionality
as reaching for the mouse and & clicking it... but this still won't
prevent the tabpages validating event from being triggered if they
forget and use the enter key instead of the space bar.

How can I stop the enter key triggering the tabpage's validating
event? Is it possible to get my button to ignore the enter key ?- it
seems impossible as the validating event is triggered before the click
event and the keypress event is not triggered by the enter key at all!

Thanks for your patience
"One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> wrote in message news:<e2**************@TK2MSFTNGP10.phx.gbl>...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ButtonDualHandler()
End Sub

Private Sub Button1_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

'Determines if return is pressed and then calls
'Our button handler if so
If e.KeyChar = Convert.ToChar(Keys.Return) Then
ButtonDualHandler()
End If

'prevents the normal handling of this event
e.Handled = True

End Sub

Private Sub ButtonDualHandler()
MsgBox("I got pressed or clicked")
End Sub

Private Sub Form1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
MsgBox("Validating")
End Sub

Private Sub Form1_Validated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Validated
MsgBox("Validated")
End Sub

HTH

Nov 21 '05 #8

P: n/a
What I have discovered is that if you trap the keyPress/Down/Upo events,
they never get fired on a Tab Page. If you disable the Causes Validation on
the TabControl ( not the Tab Pages ), pressing the Enter key has the effect
of firing the Buttons_Click Event.

HTH

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"SJ" <su****************@yahoo.co.uk> wrote in message
news:3d**************************@posting.google.c om...
hello again,

the below works just fine on a plain form but try it when the controls
are located on a tabpage on the form and you will see that pressing
the enter key on the button calls the tabpages validating event before
the **click** eventhandler . [Interestingly pressing the space bar
when focused on the button works as desired and just calls the
**keypress** event handler & not the tabpages validating event]

At this stage I'm happy just to tell my keyboard-friendly users to use
the space bar when focused on the button to get the same functionality
as reaching for the mouse and & clicking it... but this still won't
prevent the tabpages validating event from being triggered if they
forget and use the enter key instead of the space bar.

How can I stop the enter key triggering the tabpage's validating
event? Is it possible to get my button to ignore the enter key ?- it
seems impossible as the validating event is triggered before the click
event and the keypress event is not triggered by the enter key at all!

Thanks for your patience
"One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> wrote in
message news:<e2**************@TK2MSFTNGP10.phx.gbl>...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ButtonDualHandler()
End Sub

Private Sub Button1_KeyPress(ByVal sender As System.Object, ByVal e
As
System.Windows.Forms.KeyPressEventArgs)

'Determines if return is pressed and then calls
'Our button handler if so
If e.KeyChar = Convert.ToChar(Keys.Return) Then
ButtonDualHandler()
End If

'prevents the normal handling of this event
e.Handled = True

End Sub

Private Sub ButtonDualHandler()
MsgBox("I got pressed or clicked")
End Sub

Private Sub Form1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
MsgBox("Validating")
End Sub

Private Sub Form1_Validated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Validated
MsgBox("Validated")
End Sub

HTH

Nov 21 '05 #9

P: n/a
SJ
Hi again OHM,

thanks for the below suggestion I tried this and it didn't work or
didn't work consistantly.. what I've eventually done - and this is a
little clumsy - but is now working the way I want is the following:

Created a custom control button in which I put the following

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Select Case CType(msg.WParam.ToInt32, Keys)
Case Keys.Enter
bln_enterKeyPressed = True
Return True
Case Else
bln_enterKeyPressed = False
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function
Public ReadOnly Property EnterKeyPressed() As Boolean
Get
Return bln_enterKeyPressed
End Get
End Property

Basically doesn't respond to the enter key but the property records
that the
enter key was pressed & then in the form that the custom control
button sits on:-

Private Sub cc_TabPageButton_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles cc_TabPageButton.KeyUp
If e.KeyCode = Keys.Tab Then ' - ignore the tabkey as the user
will have tabbed to the control
Else
If cc_TabPageButton.EnterKeyPressed Then
ButtonDualHandler()
End If
End If
End Sub

Private Sub cc_TabPageButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cc_TabPageButton.Click
ButtonDualHandler()
End Sub

Thanks again for your replies, they helped to point me in the right
direction

Regards
Suzanne

"One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> wrote in message news:<#H*************@TK2MSFTNGP11.phx.gbl>...
What I have discovered is that if you trap the keyPress/Down/Upo events,
they never get fired on a Tab Page. If you disable the Causes Validation on
the TabControl ( not the Tab Pages ), pressing the Enter key has the effect
of firing the Buttons_Click Event.

HTH

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--

Nov 21 '05 #10

P: n/a
Good Idea, It would be good to get an explaination for this behaviour
though. I suggest that you repost the question having been through the
excercise, perhaps one of the other regulars can come up with an
explaination as to how this should work, or why it works the way it does,
because frankly Im at a loss with this one.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"SJ" <su****************@yahoo.co.uk> wrote in message
news:3d**************************@posting.google.c om...
Hi again OHM,

thanks for the below suggestion I tried this and it didn't work or
didn't work consistantly.. what I've eventually done - and this is a
little clumsy - but is now working the way I want is the following:

Created a custom control button in which I put the following

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Select Case CType(msg.WParam.ToInt32, Keys)
Case Keys.Enter
bln_enterKeyPressed = True
Return True
Case Else
bln_enterKeyPressed = False
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
End Function
Public ReadOnly Property EnterKeyPressed() As Boolean
Get
Return bln_enterKeyPressed
End Get
End Property

Basically doesn't respond to the enter key but the property records
that the
enter key was pressed & then in the form that the custom control
button sits on:-

Private Sub cc_TabPageButton_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles cc_TabPageButton.KeyUp
If e.KeyCode = Keys.Tab Then ' - ignore the tabkey as the user
will have tabbed to the control
Else
If cc_TabPageButton.EnterKeyPressed Then
ButtonDualHandler()
End If
End If
End Sub

Private Sub cc_TabPageButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cc_TabPageButton.Click
ButtonDualHandler()
End Sub

Thanks again for your replies, they helped to point me in the right
direction

Regards
Suzanne

"One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> wrote in
message news:<#H*************@TK2MSFTNGP11.phx.gbl>...
What I have discovered is that if you trap the keyPress/Down/Upo events,
they never get fired on a Tab Page. If you disable the Causes Validation
on
the TabControl ( not the Tab Pages ), pressing the Enter key has the
effect
of firing the Buttons_Click Event.

HTH

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--

Nov 21 '05 #11

P: n/a
If this is VB6 you can set the button's "Default" property to true. That
causes the Enter key to "press" the button. If it is not VB6 there may still
be the rquivilent functionality. You might want to check it out.

"SJ" wrote:
Hope someone can help me out there I'm struggling with a particular
problem...

I have a form with many tab pages. On one tab page I've got a button
which when clicked with a mouse adds items into a datagrid. When a
user presses the enter key on this button I want the mouse click event
to be activated but instead the tab page's validating event is called.

I understand that this behaviour is by partly by design (Accept button
etc) but I have not set an accept button for my form (because of the
many tab pages there is more than one button so an accept or a cancel
button is not viable).

In short - for my users who prefer the keyboard to the mouse - I want
an enter key press on a button to act like a button click using the
mouse.
How do I do this?

(Note as a part attempt to fix, I've tried setting causesvalidation
to false for my button to prevent it from calling the tab pages
validating event and this did not work - the validating event was
still triggered)

many thanks
Suzanne

Nov 21 '05 #12

P: n/a
Hi,
VB has the Default property belonging to the control.
Dot Net has "Accept Button" and "Cancel Button" properties on the form
allowing selection of the control. Makes better sense.

Doug

"Warmrain" <Wa******@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
If this is VB6 you can set the button's "Default" property to true. That
causes the Enter key to "press" the button. If it is not VB6 there may still be the rquivilent functionality. You might want to check it out.

"SJ" wrote:
Hope someone can help me out there I'm struggling with a particular
problem...

I have a form with many tab pages. On one tab page I've got a button
which when clicked with a mouse adds items into a datagrid. When a
user presses the enter key on this button I want the mouse click event
to be activated but instead the tab page's validating event is called.

I understand that this behaviour is by partly by design (Accept button
etc) but I have not set an accept button for my form (because of the
many tab pages there is more than one button so an accept or a cancel
button is not viable).

In short - for my users who prefer the keyboard to the mouse - I want
an enter key press on a button to act like a button click using the
mouse.
How do I do this?

(Note as a part attempt to fix, I've tried setting causesvalidation
to false for my button to prevent it from calling the tab pages
validating event and this did not work - the validating event was
still triggered)

many thanks
Suzanne

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.