CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    17

    Text Box executes method after ENTER

    Hi All,

    I have a text box in my application. I want my program to execute a method only when the user has keyboarded "return". At present I have it working when the user edits the text in the text box, the method is called but I want it only to call the method after the user has pressed return?

    Can you help me with this?

    I am using Visual C#2008 to build my application.

    Thanks in advance.......

    Paul

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Text Box executes method after ENTER

    Handle the TextBox.KeyDown event, and then check the e.KeyCode property for Keys.Enter. If the user pressed enter, call e.SuppressKeyPress = true to prevent that ding sound when the key is pressed and then call the method you want to call. Example:

    Code:
    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
       if (e.KeyCode == Keys.Enter)
       {
          e.SuppressKeyPress = true; // Prevent the ding sound and stop the message pump
          MyMethodCall(); // Make the call to the method you want to run
       }
    }
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Nov 2008
    Posts
    17

    Re: Text Box executes method after ENTER

    Thanks mate.....

    That works perfectly- much appreciated...

    Cheers,

    Paul.

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Text Box executes method after ENTER

    No problem, I use it all the time. Its always been a beautiful solution
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured