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

S

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
 
O

One Handed Man \( OHM - Terry Burns \)

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))
 
S

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
 
O

One Handed Man \( OHM - Terry Burns \)

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))
 
O

One Handed Man \( OHM - Terry Burns \)

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))
 
S

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
 
O

One Handed Man \( OHM - Terry Burns \)

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 said:
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 \) said:
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
 
S

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
 
O

One Handed Man \( OHM - Terry Burns \)

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))
 
G

Guest

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.
 
D

Doug Bell

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
 

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