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

后端 未结 7 1677
再見小時候
再見小時候 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:00

    Multiple selection is not supported, but i got here by search to find the CheckedItems.

    The selected items refers to the items that are marked, the checked items refers to the items that are checked.

    Hence use .CheckedItems property instead of .SelectedItems if you want the items with a checked checkbox.

    0 讨论(0)
  • 2020-12-30 09:12

    There might be an easier alternative, but you could use a ListView, set CheckBoxes to true, HeaderStyle to None, and View to List.

    Correction:

    Should have been set View to Details.

    0 讨论(0)
  • 2020-12-30 09:15

    It appears that it it not possible to set see the remarks section in CheckedListBox.SelectionMode Property

    For an easier alternative follow the adivice of adrift.

    0 讨论(0)
  • 2020-12-30 09:16

    for the multichecks i came up with this today:

        List<int> listBox2_selectionhistory = new List<int>();
    
        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)

    0 讨论(0)
  • 2020-12-30 09:19

     private System.Windows.Forms.CheckedListBox LBO1;
    
     string mySentLst = string.Join(";", LBO1.CheckedItems.Cast<string>());
    
    0 讨论(0)
  • 2020-12-30 09:19

    Please follow these steps:

    • Select CheckOnClick = true.

    • When you want to retrieve selected item, use GetItemChecked(int index) method instead.

    0 讨论(0)
提交回复
热议问题