Click button when pressing enter key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erwinnnn
    New Member
    • Mar 2008
    • 2

    Click button when pressing enter key

    I'm a total noob on c#, i just started 2 weeks ago
    I'm making a calculator

    Its very simple..
    When i press the enter key, button1 must be clicked
    It works with all the other keys, but with the enter key it DOESN'T WORK :(
    I've searched on google for hours and i tried almost everything, but nothing works..

    So please, help me
    Thx
    Last edited by sicarie; Mar 21 '08, 02:25 AM. Reason: Moved to .NET Forum
  • int08h
    New Member
    • Apr 2007
    • 28

    #2
    if you are developing a WinForm, the "AcceptButt on" property of the form makes sense.

    Comment

    • shweta123
      Recognized Expert Contributor
      • Nov 2006
      • 692

      #3
      Hi,

      You can refer This Article for handling keypress event for Win Form.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Take the code out of your button click event and create a sub called Totalize or something descriptive. Put the code from the button click event in this sub
        Code:
        Public Sub Form1_KeyPress(....) Handles Form1.Keypress
          If (e.KeyCode = Keys.Enter) Or (e.KeyCode = Keys.Return) Then
        	Totalize()
          End If
        End Sub
         
        Public Sub Button1_Click(....) Handles Button1.Click
          Totalize()
        End Sub
        That should do what you're looking for.

        Comment

        • erwinnnn
          New Member
          • Mar 2008
          • 2

          #5
          Ok, thank you for your help

          Comment

          Working...