C# 中删除 PictureBox 中的图像
当用户按“del”键时如何从图片框中删除图像...我没有找到 PB 的任何按键或按键事件。
private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
{
imgSelected=true;
//need to accept "delete"key from keyboard?
topRight_pbx.Image = null;
topRFile = "";
}
how to delete image from picture box when user press "del" key...I dont find any keypress or keydown events for PB.
private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
{
imgSelected=true;
//need to accept "delete"key from keyboard?
topRight_pbx.Image = null;
topRFile = "";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的 imgSelected 更改为类似以下内容:
在图片框中单击将此变量设置为发送者:
然后在表单的按键或具有焦点的控件上运行图像删除代码
(表格示例):
Change your imgSelected to something like:
On your picturebox click set this variable to the sender:
Then on the keydown of form or the control that has focus you run the image removal code
(Example for form):
PictureBox
控件永远无法获得焦点,并且非焦点控件不会接收键盘输入事件。这是因为 microsoft.com/en-us/library/system.windows.forms.picturebox.keydown.aspx" rel="nofollow">文档 显示,
KeyDown
事件(以及其他事件与键盘输入相关的)被标记为[BrowsableAttribute(false)]
因为它们不能按预期工作。它们不打算由您的代码订阅。它类似于
Label
控件 - 您可以查看它,但它不可选择且无法获取焦点。您需要找到另一种方式让用户表明他想要删除当前显示在
PictureBox
控件中的图像。That's because the
PictureBox
control can never get the focus, and non-focused controls do not receive keyboard input events.As the documentation shows, the
KeyDown
event (and the other events related to keyboard input) are marked with[BrowsableAttribute(false)]
because they do not work as expected. They are not intended to be subscribed to by your code.It's similar to a
Label
control—you can look at it, but it's not selectable and can't acquire the focus.You'll need to find another way for the user to indicate that (s)he wants to delete an image currently displayed in a
PictureBox
control.我在我的一个项目中遇到了类似的问题。我通过添加一个离屏文本框解决了这个问题。当单击某些控件时,我将焦点放在文本框上,并使用文本框来处理键盘输入。
I had a similar problem in one of my projects. I solved it by adding an off-screen textbox. I give focus to the text-box when certain controls get clicked, and use the text-box to handle the keyboard input.
另一种方法可能是:
如果您在 pictureBox 上绘图并且想要清除它:
之后您可以在控件上再次绘图。
我希望这可以帮助某人
A different way for this could be:
If you are drawing on a pictureBox and you want to clear it:
After that you can draw again on the control.
I hope this can help to someone