textboxes, comboboxes, ... return key and tab key


  • In textboxes (and other controltypes, I suppose), pressing Return doesn't cause the focus to be passed to the next control (and, therefore, the events related to validation and the like to be fired. On the other hand, pressing Tab key works that way, so.... is there any way (configuration, properties,...) that would make textboxes react to Enter the same than to Tab???

    Saturday, October 15, 2005 9:01 PM

All replies

  • You can add an event handler for the KeyPress event of each textbox. Then use this code:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        if (e.KeyChar == '\r')
            e.Handled =

    Sunday, October 16, 2005 11:56 AM
  • well, thanks, but this forces to write an event handler to EACH textbox. So, if there is no other wiser method, then maybe I should derive a class from textbox (something like TextBoxAcceptingReturn class), so I need to write the event once....

    Sunday, October 16, 2005 7:42 PM
  • If you want something specific to happen on every TextBox that you add to your form, you should consider deriving your own class from the TextBox control class. Deriving your own class IS the wiser choice. ;)
    Sunday, October 16, 2005 8:12 PM