有没有办法在没有按下Ctrl键的情况下在ObjectListView中进行多选?

时间:2017-07-21 05:31:29

标签: c# multi-select objectlistview

我在ObjectListView组件中将FullRowSelect和MultiSelect属性设置为true,但我仍然需要通过按Ctrl键选择多行。那么,有没有办法在没有按下Ctrl或Shift键的情况下进行多选?

1 个答案:

答案 0 :(得分:1)

我想我有一个有效的解决方案。此示例中的模型对象的类型为" Item"。有关解释,请参阅代码注释。

// We require a list to remember which items should stay selected
private List<Item> _MultiSelectList;

public Form1() {
    // <other stuff>
    _MultiSelectList = new List<Item>();
}

// use this event to check which item has been clicked
private void objectListView1_MouseClick(object sender, MouseEventArgs e) {
    objectListView1.BeginUpdate();

    // any item clicked?
    if (objectListView1.MouseMoveHitTest.Item != null) {
        var item = objectListView1.MouseMoveHitTest.Item.RowObject as Item;

        // model object of expected type available?
        if (item != null) {
            // add or remove item from list to effectively toggle selection
            if (_MultiSelectList.Contains(item)) {
                _MultiSelectList.Remove(item);
            } else {
                _MultiSelectList.Add(item);
            }
        }
    }

    // select all desired items
    objectListView1.SelectObjects(_MultiSelectList);
    objectListView1.EndUpdate();
}

// optional: to prevent flickering from the native item selection change, we freeze the OLV contents during the mouse click
private void objectListView1_MouseDown(object sender, MouseEventArgs e) {
    objectListView1.Freeze();
}

private void objectListView1_MouseUp(object sender, MouseEventArgs e) {
    objectListView1.Unfreeze();
}