我正在尝试用winforms创建一个多选图片库。
目前我已经创建了一个flowcontrolpanel,可以将图像添加为selectablepicturebox控件。
selectablepicturebox控件是一个客户用户控件,它是一个空白控件,带有一个图片框和一个位于图片框右上角的复选框。图片框略小,并以用户控件为中心。
单击selectablepicturebox控件将打开和关闭背景指示选择。
我希望能够做的是选择一堆selectablepicturebox控件,并能够捕获空格键事件以检查和取消选中所选控件中的复选框。
问题是flowlayoutpanel永远不知道捕获空格键事件。
有没有人知道做这个或其他技术?我很高兴使用任何基于.net的技术。
由于
编辑: 以下是code
的链接答案 0 :(得分:1)
您是否正在尝试KeyDown事件?
根据MSDN,此成员对此控件没有意义。
阅读here& here。相反,您可以尝试PreviewKeyDown
解决方案:[GitHub代码库]
[代码更改] 1. SelectablePictureBox.cs - 注意设置焦点
public void SetToSelected()
{
SelectedCheckBox.Checked = true;
PictureHolder.Focus();
}
private void PictureHolder_Click(object sender, EventArgs e)
{
BackColor = BackColor == Color.Black ? Color.Transparent : Color.Black;
// TODO: Implement multi select features;
if ((Control.ModifierKeys & Keys.Shift) != 0)
{
// Set the end selection index.
}
else
{
// Set the start selection index.
}
PictureHolder.Focus();
}
// subscribe to picture box's PreviewKeyDown & expose a public event
public event PreviewKeyDownEventHandler OnPicBoxKeyDown;
private void OnPicBoxPrevKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (OnPicBoxKeyDown != null)
{
OnPicBoxKeyDown(sender, e);
}
}
[代码更改] 1. FormMain.cs
private void FormMain_Load(object sender, EventArgs e)
{
SensitiveInformation sensitiveInformation = new SensitiveInformation();
int index = 0;
//foreach (var photo in Flickr.LoadLatestPhotos(sensitiveInformation.ScreenName))
for (int i = 0; i < 10; i++)
{
SelectablePictureBox pictureBox = new SelectablePictureBox(index);
// subscribe to picture box's event
pictureBox.OnPicBoxKeyDown += new PreviewKeyDownEventHandler(pictureBox_OnPicBoxKeyDown);
PictureGallery.Controls.Add(pictureBox);
index++;
}
}
// this code does the selection. Query the FLowLayout control which is the 1st one and select all the selected ones
void pictureBox_OnPicBoxKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode != Keys.Space) return;
foreach (SelectablePictureBox item in Controls[0].Controls)
{
if (item.IsHighlighted)
{
item.SetToSelected();
}
}
}