Capturing return keypress

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    Capturing return keypress

    I've never handled keysroke events before and I would like to implement a Return keystroke event in a Windows form to bring up a list of items in a datagrid as well as habing a search button in the form.

    With the help of a tutorial, I have coded the following event:

    Code:
    private void ReturnResults(object o, KeyPressEventArgs e)
            {
                char c = e.KeyChar;
                if (c == (char) Keys.Return)
                {
                    SearchAdmin();
                }
                else
                {
                    e.Handled = false;
                }
    
                base.ProcessDialogKey(Keys.Return);
            }
    and have assigned the event handler in the constructor of my Form, with the following line:

    Code:
    this.KeyPress += new KeyPressEventHandler(ReturnResults);
    //this.ProcessDialogKey += new KeyPressEventHandler(ReturnResults);
    I have also been told that the KeyPress event does not handle control keys, so I am using the ProcessDialogKe y() library to override it in the method, but cannot assign this to the ProcessDialogKe y method group (commented out part).

    Unfortunately, nothing happens when I press return with the code as it stands. And, when I first tried implementing it, the form would EXIT when return was pressed, but I don't know how that would have happened?

    Am I missing something? Please advise.

    Matt
  • mbewers1
    New Member
    • Feb 2009
    • 68

    #2
    Solution to keypress problem

    In case anyone else has this problem:

    You need to put the following in the constructor of the form:
    Code:
    this.KeyPreview = true;
    if you want to handle a key being pressed when controls are active:
    The control will then work on the form every time!

    Comment

    • IanWright
      New Member
      • Jan 2008
      • 179

      #3
      Useful little bit of information...

      Comment

      • developing
        New Member
        • Mar 2007
        • 110

        #4
        i actually have a similar question...thou ght id ask it in this thread rather than starting a new one

        i have a infinite for loop that keeps on going. i want to make it so that if 'escape' key is pressed at anytime, the loop stops. whats the best way to do this?

        im new to multithreading, delegates, and events but im thinking of having a key press event that is fired whenever the escape key is pressed and it stops the loop. i understand the theory but not sure how the code would look.

        thuoghts? sample code? links? thanks.

        Comment

        • IanWright
          New Member
          • Jan 2008
          • 179

          #5
          Originally posted by developing
          i actually have a similar question...thou ght id ask it in this thread rather than starting a new one

          i have a infinite for loop that keeps on going. i want to make it so that if 'escape' key is pressed at anytime, the loop stops. whats the best way to do this?

          im new to multithreading, delegates, and events but im thinking of having a key press event that is fired whenever the escape key is pressed and it stops the loop. i understand the theory but not sure how the code would look.

          thuoghts? sample code? links? thanks.
          Depends how you'd like to do it and how you're code it setup. You could have

          Code:
          while(Console.ReadKey().Key != Key.Escape)
          {
             // Do stuff
          }
          or something more like:

          Code:
          private bool cancel;
          
          private void Setup()
          {
             this.KeyDown += new KeyDownEventHandler(ToggleCancel);
             
             // or using lambdas
             this.KeyDown += new KeyDownEventHandler((sender, e) => 
             {
              if(e.Key == Keys.Escape)
                {
                   this.cancel = true;
                }
             });
          
             Process();
          }
          
          private void Process()
          {
             cancel = false;
             while(!cancel)
             {
                // Do stuff
             }
          }
          
          private void ToggleCancel(Object sender, KeyEventArgs e)
          {
             if(e.Key == Keys.Escape)
             {
                this.cancel = true;
             }
          }

          Comment

          • developing
            New Member
            • Mar 2007
            • 110

            #6
            thanks for the skeleton, im trying the second part.

            lets see if this doesn't help me out. thanks

            Comment

            • developing
              New Member
              • Mar 2007
              • 110

              #7
              i got it working. thanks...had to put the stuff inside the while(!cancel) loop and the event on different threads to get it workin

              Comment

              Working...