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. How to focus on button.

How to focus on button.

Scheduled Pinned Locked Moved C#
tutorial
6 Posts 5 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.
  • Y Offline
    Y Offline
    Yogesh Tayade
    wrote on last edited by
    #1

    Hi all, I have a textbox and one button in my form. After entering text in textbox I have to click on Ok button. Insted of click on Ok button I want to press enter.

    S M L 3 Replies Last reply
    0
    • Y Yogesh Tayade

      Hi all, I have a textbox and one button in my form. After entering text in textbox I have to click on Ok button. Insted of click on Ok button I want to press enter.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      What about using a KeyPreviewDown event handler for the text box, then you can use the same code you use in your button Click event handler

      Life goes very fast. Tomorrow, today is already yesterday.

      P 1 Reply Last reply
      0
      • Y Yogesh Tayade

        Hi all, I have a textbox and one button in my form. After entering text in textbox I have to click on Ok button. Insted of click on Ok button I want to press enter.

        S Offline
        S Offline
        SeMartens
        wrote on last edited by
        #3

        Register for the KeyDown-Event of your textbox. Within the handler check if the KeyCode equals Enter and call your OK-method.

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        1 Reply Last reply
        0
        • M musefan

          What about using a KeyPreviewDown event handler for the text box, then you can use the same code you use in your button Click event handler

          Life goes very fast. Tomorrow, today is already yesterday.

          P Offline
          P Offline
          Pauwels Bart
          wrote on last edited by
          #4

          Hi, You can do it like this.

          textBox.PreviewKeyDown += new KeyEventHandler(textBox_PreviewKeyDown);

          private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
          {
          // Enter without modifiers.
          if (e.Key == Key.Return && Keyboard.Modifiers == ModifierKeys.None)
          {
          DoSameThingAsOKButton();

          e.Handled = true;
          
          return;
          

          }
          }

          Hope this helps, Bart

          M 1 Reply Last reply
          0
          • P Pauwels Bart

            Hi, You can do it like this.

            textBox.PreviewKeyDown += new KeyEventHandler(textBox_PreviewKeyDown);

            private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
            {
            // Enter without modifiers.
            if (e.Key == Key.Return && Keyboard.Modifiers == ModifierKeys.None)
            {
            DoSameThingAsOKButton();

            e.Handled = true;
            
            return;
            

            }
            }

            Hope this helps, Bart

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            I think you have replied to the wrong persona

            Life goes very fast. Tomorrow, today is already yesterday.

            1 Reply Last reply
            0
            • Y Yogesh Tayade

              Hi all, I have a textbox and one button in my form. After entering text in textbox I have to click on Ok button. Insted of click on Ok button I want to press enter.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You don't need any code for this, just: - make sure TextBox.AcceptsReturn is false - have an OK button with DialogResult not equal to None - set your form's AcceptButton to that OK button. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              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