开发者

Handling KeyDown for Label and PictureBox

开发者 https://www.devze.com 2023-03-29 19:18 出处:网络
I\'m using PictureBox to place and relocate (move) some picture inside Main Dialog Form. I want to be able to remove the picture on Delete Key开发者_C百科 pressed.

I'm using PictureBox to place and relocate (move) some picture inside Main Dialog Form. I want to be able to remove the picture on Delete Key开发者_C百科 pressed. I can't find the keyboard handling events for the listed controls.

Well, I can use KeyDown for the Form and check if the PictureBox is focused, but it looks ugly.

Can see like it has the event http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.keydown.aspx

But in fact doesn't...

Any idea?


It is a not-so-subtle hint that neither of those controls can get the focus. Keyboard events are only ever generated for controls that can be focused. TextBox being the ultimate example, but many others do. They have a way to indicate the focus to the user, typically with the dotted focus rectangle, a color or a caret.

But not PictureBox nor Label, you can click on them all you want but they won't take the focus and won't indicate it. They are just little peons that respectively show an image or text. The user doesn't expect them to do anything interesting.

I can't tell what kind of focus behavior you want to give them, but an example of turning a non-focusable control into a focusable one is in this answer.


Since the picturebox won't get the focus, look at the form and see where the focus will likely (or possibly) be when the user presses Delete.

Decide which controls on the form you'd like to have the delete key work from (buttons, checkboxes, etc., but maybe not text boxes), and use the keydown event in those controls to take the appropriate action.

The keydown event for the form doesn't always work the way you expect, so using the keydown event in the form's individual controls may be better.

0

精彩评论

暂无评论...
验证码 换一张
取 消