How do I allow the user to multi-check with the CheckedListBox through the 'shift' key?

后端 未结 7 1770
再見小時候
再見小時候 2020-12-30 08:41

Say that I have a CheckedListBox with items \"1\", \"2\", \"3\", \"4\", and \"5\" in that order and I want to select \"2\", \"3\", and \"4\" by selecting \"2\" then holding

7条回答
  •  忘掉有多难
    2020-12-30 09:16

    for the multichecks i came up with this today:

        List listBox2_selectionhistory = new List();
    
        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int actualcount = listBox2_selectionhistory.Count;
            if (actualcount == 1)
            {
                if (Control.ModifierKeys == Keys.Shift)
                {
                    int lastindex = listBox2_selectionhistory[0];
                    int currentindex = checkedListBox2.SelectedIndex;
                    int upper = Math.Max(lastindex, currentindex) ;
                    int lower = Math.Min(lastindex, currentindex);
                    for (int i = lower; i < upper; i++)
                    {
                        checkedListBox2.SetItemCheckState(i, CheckState.Checked);
                    }
                }
                listBox2_selectionhistory.Clear();
                listBox2_selectionhistory.Add(checkedListBox2.SelectedIndex);
            }
            else
            {
                listBox2_selectionhistory.Clear();
                listBox2_selectionhistory.Add(checkedListBox2.SelectedIndex);
            }
        }
    

    as far as i know checkedlistboxes' SelectionMode can only be either one or none which means you can never make the app select more than 1 at a time (I also used this behavior to simplify my code for checkedlistboxes)

提交回复
热议问题