将全选快捷键(Ctrl + A)添加到.net列表视图中?

就像主题说我有一个列表视图,我想添加Ctrl + A选择它的所有快捷方式。 我的第一个问题是我无法弄清楚如何以编程方式选择列表视图中的所有项目。 看起来它应该相对容易,比如ListView.SelectAll()ListView.Items.SelectAll() ,但似乎并非如此。 我的下一个问题是如何定义ListView的键盘快捷方式。 我是否在KeyUp事件中执行此操作,但是您如何一次检查两个按键? 或者它是你设置的属性?

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

你可以用这样的东西完成两件事:

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

这些适用于小型列表,但如果虚拟列表中有100,000个项目,则可能需要很长时间。 这可能是你的目的矫枉过正,但以防万一::

 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); ///  /// Select all rows on the given listview ///  /// The listview whose items are to be selected public static void SelectAllItems(ListView list) { NativeMethods.SetItemState(list, -1, 2, 2); } ///  /// Deselect all rows on the given listview ///  /// The listview whose items are to be deselected public static void DeselectAllItems(ListView list) { NativeMethods.SetItemState(list, -1, 2, 0); } ///  /// Set the item state on the given item ///  /// The listview whose item's state is to be changed /// The index of the item to be changed /// Which bits of the value are to be set? /// The value to be set 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); 
 private void listView1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == (Keys.A | Keys.Control)) foreach (ListViewItem item in listView1.Items) item.Selected = true; } 

在Ctrl + A上执行,而不是在Ctrl + Shift + A等其他组合上执行。

如果用户首先释放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; } } } 

我无法评论第一个问题,但是使用KeyDown的解决方案对我来说不起作用,因为系统会立即对LeftCtrl做出反应,因此会跳过CTRL + A. 从另一侧KeyUp正常工作。

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