KeyDown Event C# WinForm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmccomic
    New Member
    • Apr 2008
    • 5

    KeyDown Event C# WinForm

    I am trying scroll horizontal scroll bars with the left and right arrow keys. I have tried registering the KeyDown and OnKeyDown events but with no success. Any help will be greatly appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Which control are you attaching the keydown event listeners?

    Comment

    • jmccomic
      New Member
      • Apr 2008
      • 5

      #3
      Originally posted by Plater
      Which control are you attaching the keydown event listeners?
      I have UserControl that is DockStyle Fill on a Form. I am attaching the keydown events to the UserControl. Like this: ("this" is the UserControl)

      protected override void OnKeyDown(KeyEv entArgs e)
      {
      if (e.KeyValue == Keys.Right)
      this.Horizontal Scroll.Value = this.Horizontal Scroll.Minimum;
      else if (e.KeyValue == Keys.Left)
      this.Horizontal Scroll.Value = this.Horizontal Scroll.Maximum;
      base.OnKeyDown( e);
      }

      Comment

      • rjvrnjn
        New Member
        • Apr 2007
        • 26

        #4
        Originally posted by jmccomic
        I have UserControl that is DockStyle Fill on a Form. I am attaching the keydown events to the UserControl. Like this: ("this" is the UserControl)
        Have you set the form's KeyPreview property to True? If not, the key events will not be trapped.

        Comment

        • jmccomic
          New Member
          • Apr 2008
          • 5

          #5
          Originally posted by rjvrnjn
          Have you set the form's KeyPreview property to True? If not, the key events will not be trapped.
          Sorry about that the UserControl is inside of a panel and the panel is on the form. I did set the form's KeyPreview property to true but did not seem to help.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            If you user control has the focus it should work.
            If the focus is on a textbox or some other control that would normally handle arrow keys, I think it will fail.
            Also, isn't your logic backwards on that? You have the Right arrow key scroll left and the Left arrow key scrolling right.

            Have you checked to see if that event is even getting fired? Maybe the key you are pressing shows up as a different key?

            Comment

            • jmccomic
              New Member
              • Apr 2008
              • 5

              #7
              Originally posted by Plater
              If you user control has the focus it should work.
              If the focus is on a textbox or some other control that would normally handle arrow keys, I think it will fail.
              Also, isn't your logic backwards on that? You have the Right arrow key scroll left and the Left arrow key scrolling right.

              Have you checked to see if that event is even getting fired? Maybe the key you are pressing shows up as a different key?

              Thanks, the logic was backwards and I was also having a focus issue. Thanks again for all of the help.

              Comment

              Working...