Prevent button from stealing focus

  • Thread starter Thread starter Lord Zoltar
  • Start date Start date
L

Lord Zoltar

Hello,
I have a problem with buttons automatically stealing focus from other
controls:
I have on a form a couple labels that respond to certain key events,
such as the left and right arrow. When any key is pressed, the buttons
gain focus and the labels lose focus, and the label never gets to
respond to the KeyDown event.
Is there a way to prevent the button from ever receiving focus from a
key event (the button still has to respond to clicks from the mouse)?
 
Hello,
I have a problem with buttons automatically stealing focus from other
controls:
I have on a form a couple labels that respond to certain key events,
such as the left and right arrow. When any key is pressed, the buttons
gain focus and the labels lose focus, and the label never gets to
respond to the KeyDown event.
Is there a way to prevent the button from ever receiving focus from a
key event (the button still has to respond to clicks from the mouse)?

Your problem doesn't seem to be "button stealing focus" som much as
"labels losing focus".
Since Labels can't get focus from the start (label.CanFocus is a
ReadOnly property always set to false), your button gets focus by
default.
Maybe you should use Textboxes instead? Or listening to key events
elsewhere?
 
Lord said:
Hello,
I have a problem with buttons automatically stealing focus from other
controls:
I have on a form a couple labels that respond to certain key events,
such as the left and right arrow. When any key is pressed, the buttons
gain focus and the labels lose focus, and the label never gets to
respond to the KeyDown event.
Is there a way to prevent the button from ever receiving focus from a
key event (the button still has to respond to clicks from the mouse)?

See if setting the tabstop property of the button to False achieves what
you want.
 
See if setting the tabstop property of the button to False achieves what
you want.

And completely screws keyboard users in the process.

Zoltar, why not make a generic keyboard handler and hook up the button's key
events to that as well? That way, even if the button has the focus, you can
still handle the arrow keys as you want.
 
Your problem doesn't seem to be "button stealing focus" som much as
"labels losing focus".
Since Labels can't get focus from the start (label.CanFocus is a
ReadOnly property always set to false), your button gets focus by
default.
Maybe you should use Textboxes instead? Or listening to key events
elsewhere?

Hmm I didn't know labels couldn't receive focus. That is probably the
root of the problem. I could try to listen to the key events in the
parent container, that might work, but the parent is a Panel, I'm not
sure Panels receive key events.
 
Back
Top