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. Advancing On Pressing Enter

Advancing On Pressing Enter

Scheduled Pinned Locked Moved C#
debuggingjsonquestion
9 Posts 6 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.
  • R Offline
    R Offline
    Roger Wright
    wrote on last edited by
    #1

    I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?

    "A Journey of a Thousand Rest Stops Begins with a Single Movement"

    A L D H Richard Andrew x64R 5 Replies Last reply
    0
    • R Roger Wright

      I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?

      "A Journey of a Thousand Rest Stops Begins with a Single Movement"

      A Offline
      A Offline
      Anshul R
      wrote on last edited by
      #2

      In the KeyDown event for the form, see if the key hit is enter and if so, write code to proceed

      1 Reply Last reply
      0
      • R Roger Wright

        I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?

        "A Journey of a Thousand Rest Stops Begins with a Single Movement"

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I do not know how to avoid sound. But you can achieve the behavior by tracking keypress event

        private void radioButton1_KeyPress(object sender, KeyPressEventArgs e)
        {
        if (e.KeyChar == 13)
        {
        Yourcontrol.Focus();
        }
        }

        HTH

        Jinal Desai - LIVE Experience is mother of sage....

        R 1 Reply Last reply
        0
        • R Roger Wright

          I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?

          "A Journey of a Thousand Rest Stops Begins with a Single Movement"

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          Hy, Use the KeyDown event and compare:

          if(e.KeyCode == Keys.Enter){
          e.Handled = true;//stops the beep/bonk noise
          e.SupressKeyPress = true;//it bypasses the KeyPress event

          txtYouNameIt.Focus();//focuses on the control you specify
          }

          Something like that should work

          Just an irritated, ranting son of ... an IT guy. At your trolling services

          R 1 Reply Last reply
          0
          • L Lost User

            I do not know how to avoid sound. But you can achieve the behavior by tracking keypress event

            private void radioButton1_KeyPress(object sender, KeyPressEventArgs e)
            {
            if (e.KeyChar == 13)
            {
            Yourcontrol.Focus();
            }
            }

            HTH

            Jinal Desai - LIVE Experience is mother of sage....

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #5

            Thanks, Jinal! It works great! :-D

            "A Journey of a Thousand Rest Stops Begins with a Single Movement"

            1 Reply Last reply
            0
            • D Dan Mos

              Hy, Use the KeyDown event and compare:

              if(e.KeyCode == Keys.Enter){
              e.Handled = true;//stops the beep/bonk noise
              e.SupressKeyPress = true;//it bypasses the KeyPress event

              txtYouNameIt.Focus();//focuses on the control you specify
              }

              Something like that should work

              Just an irritated, ranting son of ... an IT guy. At your trolling services

              R Offline
              R Offline
              Roger Wright
              wrote on last edited by
              #6

              Thanks! This is a little more general solution, and good for a User Control intended for re-use.

              "A Journey of a Thousand Rest Stops Begins with a Single Movement"

              D 1 Reply Last reply
              0
              • R Roger Wright

                Thanks! This is a little more general solution, and good for a User Control intended for re-use.

                "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                D Offline
                D Offline
                Dan Mos
                wrote on last edited by
                #7

                np :)

                Just an irritated, ranting son of ... an IT guy. At your trolling services

                1 Reply Last reply
                0
                • R Roger Wright

                  I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?

                  "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #8

                  Hi Roger, The answers give already, work perfectly well in circumstances where you know the Name of the control that you want to go to and are prepared to write an individual KeyDown/Keypress (select your poison) handler for each control. However, for circumstances where you want to simply follow the TabOrder you have already set and, where applicable, use one handler for several controls, the System.Windows.Forms.Control class has a built in method to do exactly what you want. Look up the System.Windows.Forms.Control.GetNextControl() method. An example:

                    private void Comunal\_KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e)
                    {
                       if (e.KeyCode == Keys.Enter)
                       {
                           e.Handled = true;
                           // true means go forwards
                           this.GetNextControl((Control)sender, true).Focus();
                       }
                    }
                  

                  This way you can assign the handler to all the controls in your User Control, provided that you are not already using their KeyDown event for something else.

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

                  1 Reply Last reply
                  0
                  • R Roger Wright

                    I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?

                    "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                    Richard Andrew x64R Offline
                    Richard Andrew x64R Offline
                    Richard Andrew x64
                    wrote on last edited by
                    #9

                    Everything already said will work, but you might also want to consider the "KeyPreview" property of the Form. This lets the Form see the key-press before it goes to any controls, so it allows you to create a handler at the Form level instead of a handler for each individual control. Another advantage is that all your code will be inside a single handler instead of spread out among all the various controls.

                    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