0

我正在尝试使用 winforms 创建一个多选图片库。

目前我已经创建了一个将图像添加为可选图片框控件的流程控制面板。

selectablepicturebox 控件是一个客户用户控件,它是一个空白控件,带有一个图片框和一个位于图片框右上角的复选框。图片框略小,位于用户控件的中心。

单击 selectablepicturebox 控件将打开和关闭背景指示选择。

我想要做的是选择一堆 selectablepicturebox 控件并能够捕获空格键事件以选中和取消选中所选控件中的复选框。

问题是 flowlayoutpanel 永远不知道要捕获空格键事件。

有谁知道这样做或其他技术?我很乐意使用任何基于 .net 的技术。

谢谢

编辑:这是代码的链接

4

1 回答 1

1

你在尝试 KeyDown 事件吗?

根据 MSDN,此成员对此控件没有意义。

在这里这里阅读。相反,您可以尝试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();
                }
            }
        }
于 2012-05-30T20:42:04.337 回答