0

I'm trying to create a multiselect picturegallery with winforms.

Currently I have created a flowcontrolpanel that adds images as a selectablepicturebox control.

The selectablepicturebox control is a customer usercontrol that is a blank control with a picturebox and a checkbox on the top right of the picturebox. The picturebox is slightly smaller and centered in the usercontrol.

Clicking on the selectablepicturebox control will turn the background on and off indication selection.

What I want to be able to do is to select a bunch of selectablepicturebox controls and be able to capture the spacebar event to check and uncheck the checkboxes in the selected controls.

The problem is that the flowlayoutpanel never knows to capture the spacebar event.

Does anyone know away of doing this or another technology? I'm happy to use any .net based tech.

Thanks

EDIT: Here is a link to the code

1 Answers1

1

Are you trying the KeyDown event ?

As per MSDN, This member is not meaningful for this control.

Read here & here. Instead, you may try PreviewKeyDown

Solution: [The GitHub codebase]

enter image description here

[Code Changes] 1. SelectablePictureBox.cs - NOTE the Set Focus

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);
            }
        }

[Code Changes] 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();
                }
            }
        }
Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89
  • I believe KeyPress has this info too. http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx – Brad May 30 '12 at 20:51
  • I have already given the list in this link above - http://msdn.microsoft.com/en-us/library/system.windows.forms.flowlayoutpanel_events – Angshuman Agarwal May 30 '12 at 21:03
  • I am currently trying the keydown event in the panel. But when I press space it is never captures. The button controls above and below the flowlayoutpanel will activate instead. ie. if currently the "cursor/tab" is on the first button, even though I click to select the picture boxes in my flowlayoutpanel pressing spacebar only activates the buttons in the form. – Slow Monkey King May 30 '12 at 21:03
  • You need to handle the focus properly. PreviewKeyDown Occurs before the KeyDown event when a key is pressed **while focus is on this control**. – Angshuman Agarwal May 30 '12 at 21:17
  • How do I ensure focus is on that control? What if the focus was on a control within the control? – Slow Monkey King May 30 '12 at 21:29
  • Done..I will update my answer with whatever changes I have made to your code ! – Angshuman Agarwal May 30 '12 at 22:26