Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SJ - 07 Oct 2004 17:39 GMT
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
One Handed Man \( OHM - Terry Burns \) - 07 Oct 2004 18:01 GMT
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.

Signature

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

> Hope someone can help me out there I'm struggling with a particular
> problem...
[quoted text clipped - 21 lines]
> many thanks
> Suzanne
Eric Sabine - 07 Oct 2004 22:12 GMT
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))
Iain Mcleod - 07 Oct 2004 22:46 GMT
Rather!

> Hilarious!
>
[quoted text clipped - 3 lines]
>> Next
>> Process.Start("mailto:" & New String(ch))
One Handed Man \( OHM - Terry Burns \) - 08 Oct 2004 08:48 GMT
Well, at least it keeps the auto spammers at bay !

;-D

Signature

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

> Hilarious!
>
[quoted text clipped - 3 lines]
>> Next
>> Process.Start("mailto:" & New String(ch))
SJ - 08 Oct 2004 08:41 GMT
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

> 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.
One Handed Man \( OHM - Terry Burns \) - 08 Oct 2004 09:05 GMT
   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

Signature

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

> Hi OHM
>
[quoted text clipped - 24 lines]
>> want to call it from a mouse click for a given control as well you can do
>> that also.
SJ - 08 Oct 2004 15:33 GMT
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

> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
[quoted text clipped - 30 lines]
>
> HTH
One Handed Man \( OHM - Terry Burns \) - 08 Oct 2004 16:27 GMT
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

Signature

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

> hello again,
>
[quoted text clipped - 53 lines]
>>
>> HTH
SJ - 11 Oct 2004 11:06 GMT
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

> 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
One Handed Man \( OHM - Terry Burns \) - 11 Oct 2004 16:40 GMT
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.

Signature

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

> Hi again OHM,
>
[quoted text clipped - 58 lines]
>>
>> HTH
Warmrain - 27 Apr 2005 23:43 GMT
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.

> Hope someone can help me out there I'm struggling with a particular
> problem...
[quoted text clipped - 21 lines]
> many thanks
> Suzanne
Doug Bell - 28 Apr 2005 07:26 GMT
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

> 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
[quoted text clipped - 25 lines]
> > many thanks
> > Suzanne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.