处理 Label 和 PictureBox 的 KeyDown

发布于 2024-11-30 12:12:03 字数 400 浏览 1 评论 0原文

我正在使用 PictureBox 在主对话框窗体中放置和重新定位(移动)一些图片。 我希望能够删除按下删除键时的图片。 我找不到列出的控件的键盘处理事件。

好吧,我可以对 Form 使用 KeyDown 并检查 PictureBox 是否获得焦点,但它看起来很难看。

可以看到它有事件 http:// msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.keydown.aspx

但事实上并没有...

有什么想法吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

刘备忘录 2024-12-07 12:12:03

这是一个不那么微妙的暗示,即这些控件都无法获得焦点。键盘事件仅针对可聚焦的控件生成。 TextBox 是最终的例子,但还有许多其他例子。它们有一种向用户指示焦点的方法,通常使用虚线焦点矩形、颜色或插入符号。

但 PictureBox 和 Label 不行,你可以点击它们,但它们不会获得焦点,也不会指示它。它们只是分别显示图像或文本的小苦工。用户并不期望他们做任何有趣的事情。

我不知道你想给他们什么样的焦点行为,但是将不可聚焦控件变成可聚焦控件的示例位于 这个答案

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.

山川志 2024-12-07 12:12:03

由于图片框不会获得焦点,因此请查看表单并查看当用户按删除键时焦点可能(或可能)在哪里。

确定您希望表单上的哪些控件(按钮、复选框等,但可能不是文本框)使用删除键,并使用这些控件中的 keydown 事件来执行适当的操作。

窗体的 keydown 事件并不总是按您期望的方式工作,因此在窗体的各个控件中使用 keydown 事件可能会更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文