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 trigger event when user press Enter key in textbox?

How to trigger event when user press Enter key in textbox?

Scheduled Pinned Locked Moved C#
databasetutorialquestion
8 Posts 4 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.
  • T Offline
    T Offline
    TrooperIronMan
    wrote on last edited by
    #1

    which of events to use, and if none of them is actually one for this purpose I though of using KeyPress, but not sure how to make expresion that check if key is Enter (and then execute some method)???

    M V V 3 Replies Last reply
    0
    • T TrooperIronMan

      which of events to use, and if none of them is actually one for this purpose I though of using KeyPress, but not sure how to make expresion that check if key is Enter (and then execute some method)???

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, The KeyDown Event is what you are looking for.

      YourTextBox.KeyDown += new KeyEventHandler(CheckKeys);

      private void CheckKeys(object sender, KeyEventArgs e)
      {
      if (e.KeyCode == Keys.Enter)
      {
      //Do what you whant!
      }
      }

      All the best, Martin

      1 Reply Last reply
      0
      • T TrooperIronMan

        which of events to use, and if none of them is actually one for this purpose I though of using KeyPress, but not sure how to make expresion that check if key is Enter (and then execute some method)???

        V Offline
        V Offline
        Vertyg0
        wrote on last edited by
        #3

        Here is my sample, but for KeyDown event: private void textBox1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Enter: DoSomeStuff(); e.SuppressKeyPress = true; return; case Keys.Escape: DoSomeStuff(); e.SuppressKeyPress = true; e.Handled = true; return; case Keys.Space: DoSomeStuff(); e.SuppressKeyPress = true; e.Handled = true; return; case Keys.B: DoSomeStuff(); e.SuppressKeyPress = true; e.Handled = true; return; } }

        M 1 Reply Last reply
        0
        • V Vertyg0

          Here is my sample, but for KeyDown event: private void textBox1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Enter: DoSomeStuff(); e.SuppressKeyPress = true; return; case Keys.Escape: DoSomeStuff(); e.SuppressKeyPress = true; e.Handled = true; return; case Keys.Space: DoSomeStuff(); e.SuppressKeyPress = true; e.Handled = true; return; case Keys.B: DoSomeStuff(); e.SuppressKeyPress = true; e.Handled = true; return; } }

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Looks like deleting youre question was not the best idea!

          V 1 Reply Last reply
          0
          • T TrooperIronMan

            which of events to use, and if none of them is actually one for this purpose I though of using KeyPress, but not sure how to make expresion that check if key is Enter (and then execute some method)???

            V Offline
            V Offline
            V 0
            wrote on last edited by
            #5

            I think you need KeyDown or KeyUp. add the event handler where you can do: if(e.KeyCode == KeyCode.Enter){ //Do something } it should be something like this... Hope this helps.

            V. If I don't see you in this world, I'll see you in the next one... And don't be late. (Jimi Hendrix)

            T 1 Reply Last reply
            0
            • M Martin 0

              Looks like deleting youre question was not the best idea!

              V Offline
              V Offline
              Vertyg0
              wrote on last edited by
              #6

              Not my fault ! :) Must be cp forum bug.

              M 1 Reply Last reply
              0
              • V Vertyg0

                Not my fault ! :) Must be cp forum bug.

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                Was just a joke! Maybe it has something todo, with the prevention of hacks, which chris maunder had to implement.

                1 Reply Last reply
                0
                • V V 0

                  I think you need KeyDown or KeyUp. add the event handler where you can do: if(e.KeyCode == KeyCode.Enter){ //Do something } it should be something like this... Hope this helps.

                  V. If I don't see you in this world, I'll see you in the next one... And don't be late. (Jimi Hendrix)

                  T Offline
                  T Offline
                  TrooperIronMan
                  wrote on last edited by
                  #8

                  Thanks I think it'll work, I wanted to do same thing but didn't know exact syntax...

                  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