Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Detecting the enter key

Detecting the enter key

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    exhaulted
    wrote on last edited by
    #1

    Hi all, I have a combo box and a text box on a form. cmb1 and txt1 for arguments sake. The tab order means that when tab is pressed focus moves from cmb1 to txt1. What i want to do is allow for the user to press the enter key and simulate the tab key being pressed, i.e. focus moves to the next control on the tab order. Any ideas? Cheers Kev

    R 1 Reply Last reply
    0
    • E exhaulted

      Hi all, I have a combo box and a text box on a form. cmb1 and txt1 for arguments sake. The tab order means that when tab is pressed focus moves from cmb1 to txt1. What i want to do is allow for the user to press the enter key and simulate the tab key being pressed, i.e. focus moves to the next control on the tab order. Any ideas? Cheers Kev

      R Offline
      R Offline
      Roger Stewart
      wrote on last edited by
      #2

      You could check for the Enter key in the combo's KeyPress event, then transfer focus to the next in the tab order.

      private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      {
          if ( e.KeyChar == (char)13 )
          {
              this.GetNextControl(this.ActiveControl, true).Focus();
          }
      }
      

      Roger Stewart "I Owe, I Owe, it's off to work I go..."

      H 1 Reply Last reply
      0
      • R Roger Stewart

        You could check for the Enter key in the combo's KeyPress event, then transfer focus to the next in the tab order.

        private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ( e.KeyChar == (char)13 )
            {
                this.GetNextControl(this.ActiveControl, true).Focus();
            }
        }
        

        Roger Stewart "I Owe, I Owe, it's off to work I go..."

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        You should actually use KeyCode.Enter. While this will probably always be character code 13, without optimization (based on the compiler) (char)13 may require additional instructions for the cast while using KeyCode.Enter will always compile to 13 without a cast. Besides, it's best to avoid "magic numbers" and makes for more elegant code. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        R 1 Reply Last reply
        0
        • H Heath Stewart

          You should actually use KeyCode.Enter. While this will probably always be character code 13, without optimization (based on the compiler) (char)13 may require additional instructions for the cast while using KeyCode.Enter will always compile to 13 without a cast. Besides, it's best to avoid "magic numbers" and makes for more elegant code. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          R Offline
          R Offline
          Roger Stewart
          wrote on last edited by
          #4

          I took this (the char 13) straight out of MSDN Library for the KeyPressEventArgs.KeyChar Property[^]. You might want to pass your suggestion on to MSDN Lib guys :-D:rose: Roger Stewart "I Owe, I Owe, it's off to work I go..."

          E 1 Reply Last reply
          0
          • R Roger Stewart

            I took this (the char 13) straight out of MSDN Library for the KeyPressEventArgs.KeyChar Property[^]. You might want to pass your suggestion on to MSDN Lib guys :-D:rose: Roger Stewart "I Owe, I Owe, it's off to work I go..."

            E Offline
            E Offline
            exhaulted
            wrote on last edited by
            #5

            Thanks Roger, Works great. Kev

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups