Prevent button from stealing focus

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)?
 
W

Wyrm

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?
 
F

Family Tree Mike

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.
 
J

Jeff Johnson

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.
 
L

Lord Zoltar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top