代码之家  ›  专栏  ›  技术社区  ›  ryanulit

是否将“全选”快捷方式(Ctrl+a)添加到.net listview?

  •  19
  • ryanulit  · 技术社区  · 15 年前

    正如主题所说,我有一个listview,我想添加 Ctrl键 + 选择它的所有快捷方式。我的第一个问题是,我不知道如何以编程方式选择listview中的所有项。看起来应该比较容易,比如 ListView.SelectAll() ListView.Items.SelectAll() ListView . 我是在一个小时内做的吗 KeyUp

    这里的任何帮助都会很好。

    5 回复  |  直到 13 年前
        1
  •  32
  •   Shane Fulmer Lasse V. Karlsen    15 年前

    您可以通过以下方式实现这两个目标:

    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
  •  24
  •   Grammarian    15 年前

    这些方法适用于小列表,但如果虚拟列表中有100000个项目,这可能需要很长时间。这可能是出于您的目的,但以防万一:

    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);
        }
    }
    

    你是这样用的::

    NativeMethods.SelectAllItems(this.myListView);
    
        3
  •  6
  •   nyrocron    11 年前
    private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == (Keys.A | Keys.Control))
            foreach (ListViewItem item in listView1.Items)
                item.Selected = true;
    }
    

    只有

        4
  •  3
  •   Jasper wontondon    13 年前

    如果用户发布 Ctrl键 钥匙在前。

    你应该使用 KeyDown 相反,事件:

    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
  •  0
  •   Vitaliy.Vol    11 年前

    我无法对第一个问题发表评论,但使用KeyDown的解决方案对我来说不起作用,因为系统会在按下LeftCtrl时立即做出反应,因此跳过CTRL+A。从另一面看,keydup可以正常工作。

    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;
            }
        }
    }