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. .NET (Core and Framework)
  4. Weird OnKeyDown bug...

Weird OnKeyDown bug...

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestion
3 Posts 2 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.
  • J Offline
    J Offline
    Jon Rista
    wrote on last edited by
    #1

    I have a strange problem with a custom control (derives from System.Windows.Control). I've added code to the protected OnKeyDown and OnKeyUp methods of my control to handle up/down/left/right keypresses. The keys work great most of the time. The one case where they do not work is when my control is used with the System.Windows.Forms.ListBox control. The listbox kills the focus on my control, preventing any keypress events from fireing. Sometimes the listbox takes the focus itself, sometimes not. Any ideas on what the deal is? :confused: Thanks.

    J 1 Reply Last reply
    0
    • J Jon Rista

      I have a strange problem with a custom control (derives from System.Windows.Control). I've added code to the protected OnKeyDown and OnKeyUp methods of my control to handle up/down/left/right keypresses. The keys work great most of the time. The one case where they do not work is when my control is used with the System.Windows.Forms.ListBox control. The listbox kills the focus on my control, preventing any keypress events from fireing. Sometimes the listbox takes the focus itself, sometimes not. Any ideas on what the deal is? :confused: Thanks.

      J Offline
      J Offline
      Jeff J
      wrote on last edited by
      #2

      Jon Rista wrote: The one case where they do not work is when my control is used with the System.Windows.Forms.ListBox control. What exactly is the relationship between your custom control and the listbox? Is on embedded within the other, are you superclassing, are they siblings on a form...?

      J 1 Reply Last reply
      0
      • J Jeff J

        Jon Rista wrote: The one case where they do not work is when my control is used with the System.Windows.Forms.ListBox control. What exactly is the relationship between your custom control and the listbox? Is on embedded within the other, are you superclassing, are they siblings on a form...?

        J Offline
        J Offline
        Jon Rista
        wrote on last edited by
        #3

        They are siblings on a form. After more testing, this seems to be a .NET issue. By default, .NET seems to shift focus when the arrow keys are pressed down. Focus is automatically taken from one control and moved to the next until it hits a control that can keep the focus. For example, if you put a button, a checkbox, a textbox, and a radio button on a form, in top-down order, you can try this: Click the button. Press down arrow three times. You will see the focus shift from the button, to the checkbox, and stop at the textbox, never reaching the radio button. Somehow the textbox is keeping the focus, although how I do not know. I need to figure out what .NET does that prevents a custom control derived from the Control class from keeping focus once it has it. Well, rather, keeping focus when the arrow keys are pressed, it keeps focus for all other key events.

        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