How do I make the enter key act like a button click? | | |
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 | | | | re: How do I make the enter key act like a button click?
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" <suzannejamesuk2003@yahoo.co.uk> wrote in message
news:3d9e6189.0410070839.dffc43a@posting.google.co m...[color=blue]
> 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[/color] | | | | re: How do I make the enter key act like a button click?
Hilarious!
[color=blue]
> 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))[/color] | | | | re: How do I make the enter key act like a button click?
Rather!
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:uzPloJLrEHA.3988@tk2msftngp13.phx.gbl...[color=blue]
> Hilarious!
>[color=green]
>> 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))[/color]
>
>[/color] | | | | re: How do I make the enter key act like a button click?
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$rU9IrEHA.4044@TK2MSFTNGP09.phx.gbl>...[color=blue]
> 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))
> --
>
>
>[/color] | | | | re: How do I make the enter key act like a button click?
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:uzPloJLrEHA.3988@tk2msftngp13.phx.gbl...[color=blue]
> Hilarious!
>[color=green]
>> 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))[/color]
>
>[/color] | | | | re: How do I make the enter key act like a button click?
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" <suzannejamesuk2003@yahoo.co.uk> wrote in message
news:3d9e6189.0410072341.5371b43d@posting.google.c om...[color=blue]
> 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$rU9IrEHA.4044@TK2MSFTNGP09.phx.gbl>...[color=green]
>> 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))
>> --
>>
>>
>>[/color][/color] | | | | re: How do I make the enter key act like a button click?
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:<e2KlY2QrEHA.1164@TK2MSFTNGP10.phx.gbl>...[color=blue]
> 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
>
>[/color] | | | | re: How do I make the enter key act like a button click?
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" <suzannejamesuk2003@yahoo.co.uk> wrote in message
news:3d9e6189.0410080633.6a62f513@posting.google.c om...[color=blue]
> 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:<e2KlY2QrEHA.1164@TK2MSFTNGP10.phx.gbl>...[color=green]
>> 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
>>
>>[/color][/color] | | | | re: How do I make the enter key act like a button click?
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:<#HHNLtUrEHA.324@TK2MSFTNGP11.phx.gbl>...[color=blue]
> 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))
> --
>
>[/color] | | | | re: How do I make the enter key act like a button click?
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" <suzannejamesuk2003@yahoo.co.uk> wrote in message
news:3d9e6189.0410110206.2e0435b4@posting.google.c om...[color=blue]
> 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:<#HHNLtUrEHA.324@TK2MSFTNGP11.phx.gbl>...[color=green]
>> 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))
>> --
>>
>>[/color][/color] | | | | re: How do I make the enter key act like a button click?
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:
[color=blue]
> 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
>[/color] | | | | re: How do I make the enter key act like a button click?
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" <Warmrain@discussions.microsoft.com> wrote in message
news:3BF0FF6B-4112-4BA9-98FB-C87334EEE3FD@microsoft.com...[color=blue]
> 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[/color]
still[color=blue]
> be the rquivilent functionality. You might want to check it out.
>
> "SJ" wrote:
>[color=green]
> > 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
> >[/color][/color] |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|