Intercepting the Enter key

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Murphy

    Intercepting the Enter key

    I have a default button on a form, but when a certain textbox has focus, I want
    to intercept Enter key events and perform a different action. Is there an easy
    way to do this, or should I just check the textbox's focus in the default
    button's event handler?

    Thanks in advance,
    Steve Murphy


  • Uchiha Jax

    #2
    Re: Intercepting the Enter key

    You could use the KeyUp or KeyDown event handler on the textbox and check
    the KeyEventArgs.Ke yCode property agains the Keys enumerable Keys.Enter. e.g

    public class Form1:System.Wi ndows.Forms.For m
    {
    private TextBox textBox1
    public Form1
    {
    /// add button and textbox to form etc

    textBox1.KeyUp += new KeyEventHandler (TextBox1_KeyUp );
    }

    private void TextBox1_KeyUp( object sender, KeyEventArgs e)
    {
    if(e.KeyCode == Keys.Enter)
    {
    RunSomeMethod() ; /// the method you wish to run
    }
    }
    }


    Comment

    • Steve Murphy

      #3
      Re: Intercepting the Enter key


      "Uchiha Jax" <i_am_anti_ever ythingNOSPAM@NO SPAMhotmail.com > wrote:
      [color=blue]
      > You could use the KeyUp or KeyDown event handler on the textbox and check
      > the KeyEventArgs.Ke yCode property agains the Keys enumerable Keys.Enter. e.g[/color]

      Thanks. This works only in the KeyUp event. Do you have any thoughts on why? Not
      that it's important; I'm just curious.

      Also, however, this does not filter the event, and the default button event
      still runs.

      Thanks,
      Steve Murphy


      Comment

      • Uchiha Jax

        #4
        Re: Intercepting the Enter key

        Oh yes sorry, far easier is to put in the button eventhandler

        if(!this.textBo x1.Focused)
        {
        RunYourRoutine( );
        }
        else
        {
        //// do something else
        }

        Although if you have lots of these types of textbox you may want to consider
        a different solution, is this ASP.NET?


        "Steve Murphy" <smurphy@granit e-ridge.com> wrote in message
        news:OdIXE2%23B FHA.1260@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        >
        > "Uchiha Jax" <i_am_anti_ever ythingNOSPAM@NO SPAMhotmail.com > wrote:
        >[color=green]
        > > You could use the KeyUp or KeyDown event handler on the textbox and[/color][/color]
        check[color=blue][color=green]
        > > the KeyEventArgs.Ke yCode property agains the Keys enumerable Keys.Enter.[/color][/color]
        e.g[color=blue]
        >
        > Thanks. This works only in the KeyUp event. Do you have any thoughts on[/color]
        why? Not[color=blue]
        > that it's important; I'm just curious.
        >
        > Also, however, this does not filter the event, and the default button[/color]
        event[color=blue]
        > still runs.
        >
        > Thanks,
        > Steve Murphy
        >
        >[/color]


        Comment

        • Steve Murphy

          #5
          Re: Intercepting the Enter key

          > Oh yes sorry, far easier is to put in the button eventhandler

          No problem. I was just wondering if there was a better way.

          Thanks again,
          Steve Murphy


          Comment

          • sadhu

            #6
            Re: Intercepting the Enter key

            That's because the AcceptEnter property of the textbox is set to false.
            If you set it to true, you'll get event notification for KeyDown also.
            You'll be able to filter it too.

            Regards
            Senthil

            Comment

            • Steve Murphy

              #7
              Re: Intercepting the Enter key

              "sadhu" <senthilkumar@w devs.com> wrote:
              [color=blue]
              > That's because the AcceptEnter property of the textbox is set to false.
              > If you set it to true, you'll get event notification for KeyDown also.
              > You'll be able to filter it too.[/color]

              Nope. Tried that. It's okay, putting the code in the button method is not a
              problem.

              Thanks,
              Steve Murphy


              Comment

              Working...