locked
button "clicked" with Enter key RRS feed

  • Question

  • I have a button1 on my application. How to create, that the code will go through button_click event handler when user presses Enter key (the same, as he would clicked with a mouse)?
    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Sunday, June 7, 2009 2:34 PM

Answers

  • public System.Windows.Forms.IButtonControl AcceptButton { get;  set; }
        Member of System.Windows.Forms.Form
    
    Summary:
    Gets or sets the button on the form that is clicked when 
    the user presses the ENTER key.
    
    Return Values:
    An System.Windows.Forms.IButtonControl that represents 
    the button to use as the accept button for the form.
    


    Set that property on your Form.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:42 AM
    • Unmarked as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    • Proposed as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    Sunday, June 7, 2009 2:41 PM
  • When Viewing Form Design, See Form's properties and set 'Accept Button' property to the button you want.


    Thanks

    - Omie
    C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:42 AM
    • Unmarked as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:47 AM
    Monday, June 8, 2009 8:10 AM
  • Something like that

    in form acceptbutton set to button1 and then...


     Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
            Select Case TabControl1.SelectedIndex
                Case 0
                    Me.AcceptButton = Button1
                Case 1
                    Me.AcceptButton = Button2
            End Select
    End Sub
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:41 AM
    Wednesday, June 10, 2009 1:24 AM

All replies

  • public System.Windows.Forms.IButtonControl AcceptButton { get;  set; }
        Member of System.Windows.Forms.Form
    
    Summary:
    Gets or sets the button on the form that is clicked when 
    the user presses the ENTER key.
    
    Return Values:
    An System.Windows.Forms.IButtonControl that represents 
    the button to use as the accept button for the form.
    


    Set that property on your Form.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:42 AM
    • Unmarked as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    • Proposed as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    Sunday, June 7, 2009 2:41 PM
  • By default, pressing Enter or Space will click a button that has the focus.
    • Marked as answer by nobugz Sunday, June 7, 2009 5:34 PM
    • Unmarked as answer by Mitja Bonca Monday, June 8, 2009 7:28 AM
    Sunday, June 7, 2009 2:48 PM

  • Yee, what about it does not have a focus? If there is more then one button, lets yes 3 (YES, No, Cancel), and with pressing Enter, the fous has to go on YES button! How to do that?

    Rudedog2, I didnt realyl understand, but what you said has something what I want. Can you be a bit more specific, please?
    thx,
    Mitja

    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Monday, June 8, 2009 7:27 AM
  • When Viewing Form Design, See Form's properties and set 'Accept Button' property to the button you want.


    Thanks

    - Omie
    C isn’t that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:42 AM
    • Unmarked as answer by Aland Li Wednesday, June 10, 2009 6:46 AM
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:47 AM
    Monday, June 8, 2009 8:10 AM
  • Ok, this works!

    But on my application I have a tabControl1 with tabPage1 and tabPage2. On tabPage1 is a button1 and on a tabPage2 is a button2. I would like to "click" them with Enter key. How to define when a tabPage1 is selected - has to use button OR a tabPage2 is selected - has to use button2?

    I am a rookie at C#, so please don`t be mad if my questions goona sound stupid...
    Monday, June 8, 2009 9:22 AM
  • Something like that

    in form acceptbutton set to button1 and then...


     Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
            Select Case TabControl1.SelectedIndex
                Case 0
                    Me.AcceptButton = Button1
                Case 1
                    Me.AcceptButton = Button2
            End Select
    End Sub
    • Marked as answer by Aland Li Wednesday, June 10, 2009 6:41 AM
    Wednesday, June 10, 2009 1:24 AM
  • You are going to need to "drill down" to the controls collection on a specific TabPage in order to use Lucifer's suggestion.

    Me.AcceptButton = Me.TabControl1.TabPages[0].Controls[0]

    The above demonstrates accessing the first TabPage, and then the first control in the Controls collection for TabPage1.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, June 10, 2009 12:50 PM