-1

Checked_List_Box

(This is how my checked list box looks ^)

I would like to implement the keyboard shortcut Ctrl + Shift + Click, in a checked_list_box. Currently my code looks like this, any ideas where to start?

The outcome I would like to have is that when I have selected one of the options in the box with a mouseclick, Ctrl + Shift + Click will select the rest of the files up to the next option I have clicked.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace SelectFiles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.CheckOnClick = true;
        }

    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            MessageBox.Show(fbd.SelectedPath);
            checkedListBox1.Items.Clear();
            string[] files = Directory.GetFiles(fbd.SelectedPath);

            foreach (string file in files)
            {
                checkedListBox1.Items.Add(file);
            }
        }
    private void button2_Click_1(object sender, EventArgs e)
    {
        List<string> list_all_excelfiles = new List<string>();
        foreach (string item in checkedListBox1.CheckedItems)
        {
            list_all_excelfiles.Add(item);
            MessageBox.Show(Path.GetFileName(item));
        }
    }
}
  • 1
    Possible duplicate of [Test if the Ctrl key is down using C#](https://stackoverflow.com/questions/4705428/test-if-the-ctrl-key-is-down-using-c-sharp) – PaulF Jun 21 '18 at 16:10

1 Answers1

1

You can achieve this using the Form.ModifierKeys in the click event

private void checkedListBox1_MouseClick(object sender, MouseEventArgs e)
{
    if (Form.ModifierKeys.HasFlag(Keys.Control) && Form.ModifierKeys.HasFlag(Keys.Shift))
    {
        //Do something
    }
}
hawkstrider
  • 4,141
  • 16
  • 27