Adding a select all shortcut (Ctrl + A) to a .net listview?

瘦欲@ 提交于 2019-12-17 19:57:41

问题


Like the subject says I have a listview, and I wanted to add the Ctrl + A select all shortcut to it. My first problem is that I can't figure out how to programmatically select all items in a listview. It seems like it should be relatively easy, like ListView.SelectAll() or ListView.Items.SelectAll(), but that doesn't appear to be the case. My next problem is how to define the keyboard shortcut for the ListView. Do I do it in a KeyUp event, but then how would you check two presses at once? Or is it a property that you set?

Any help here would be great.


回答1:


You could accomplish both with something like this:

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A && e.Control)
    {
        listView1.MultiSelect = true;
        foreach (ListViewItem item in listView1.Items)
        {
            item.Selected = true;
        }
    }
}



回答2:


These works for small lists, but if there are 100,000 items in a virtual list, this could take a long time. This is probably overkill for your purposes, but just in case::

class NativeMethods {
    private const int LVM_FIRST = 0x1000;
    private const int LVM_SETITEMSTATE = LVM_FIRST + 43;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct LVITEM
    {
        public int mask;
        public int iItem;
        public int iSubItem;
        public int state;
        public int stateMask;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string pszText;
        public int cchTextMax;
        public int iImage;
        public IntPtr lParam;
        public int iIndent;
        public int iGroupId;
        public int cColumns;
        public IntPtr puColumns;
    };

    [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessageLVItem(IntPtr hWnd, int msg, int wParam, ref LVITEM lvi);

    /// <summary>
    /// Select all rows on the given listview
    /// </summary>
    /// <param name="list">The listview whose items are to be selected</param>
    public static void SelectAllItems(ListView list) {
        NativeMethods.SetItemState(list, -1, 2, 2);
    }

    /// <summary>
    /// Deselect all rows on the given listview
    /// </summary>
    /// <param name="list">The listview whose items are to be deselected</param>
    public static void DeselectAllItems(ListView list) {
        NativeMethods.SetItemState(list, -1, 2, 0);
    }

    /// <summary>
    /// Set the item state on the given item
    /// </summary>
    /// <param name="list">The listview whose item's state is to be changed</param>
    /// <param name="itemIndex">The index of the item to be changed</param>
    /// <param name="mask">Which bits of the value are to be set?</param>
    /// <param name="value">The value to be set</param>
    public static void SetItemState(ListView list, int itemIndex, int mask, int value) {
        LVITEM lvItem = new LVITEM();
        lvItem.stateMask = mask;
        lvItem.state = value;
        SendMessageLVItem(list.Handle, LVM_SETITEMSTATE, itemIndex, ref lvItem);
    }
}

and you use it like this::

NativeMethods.SelectAllItems(this.myListView);



回答3:


private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == (Keys.A | Keys.Control))
        foreach (ListViewItem item in listView1.Items)
            item.Selected = true;
}

To only do it on Ctrl+A, not on other combinations like Ctrl+Shift+A etc.




回答4:


The first solution won't work if user releases the Ctrl key first.

You should use the KeyDown event instead:

private void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A && e.Control)
    {
        listView1.MultiSelect = true;
        foreach (ListViewItem item in listView1.Items)
        {
            item.Selected = true;
        }
    }
}



回答5:


I could not comment on the first question, however solution with KeyDown does not work for me, because system immediately reacts on LeftCtrl pressed and so skips CTRL+A. From other side KeyUp works correctly.

private void listView1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A && e.Control)
    {
        listView1.MultiSelect = true;
        foreach (ListViewItem item in listView1.Items)
        {
            item.Selected = true;
        }
    }
}


来源:https://stackoverflow.com/questions/1019388/adding-a-select-all-shortcut-ctrl-a-to-a-net-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!