473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
12 7308
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hi, Is it possible to make hitting the enter key in an ASP textbox run the code behind an ASP button on a form? I have a search page which users tend to type in the query then just hit enter...
2
by: Cindy | last post by:
Hi all you smarties out there, I'm having a little conundrum with my asp.net page Scenario: I have a form (asp.net) with no code behind (as yet). I have placed a javascript function on a...
5
by: Eric | last post by:
Hi All, I'm very experienced in traditional ASP and am new to (am learning) ASP.NET. FYI: I am initially learning ASP.NET with VB.NET to ease the transition for me. I have encountered what I...
9
by: Nate Hekman | last post by:
As I've mentioned a couple of times in the last few minutes(!), I've got a simple form with an edit box and a Submit button. If I type something in the Edit box and hit Enter I hear a click but...
5
by: ewillyb | last post by:
Hi, ASP.NET has some interesting behavior when the user hits the Enter key. If there are multiple ASP:Buttons (rendered as HTML submits) on the form, when the user hits enter, the first button's...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
7
by: Marc | last post by:
Hi, I want my user to be able to rename a button control by selcting rename from a menu. This then opens a text box in which to enter the new name in. I want the button control to...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.