如何鼠标多选支持添加到列表视图WinForm的? [英] How to add mouse multiselection support to Winform listview?
问题描述
我需要鼠标多选支持添加到一个WinForm列表视图。 (用户会点击一个项目,并将其拖动至第五,她就可以选择1至5项),我想我应该继承的ListView新的功能添加到它,但我不知道哪些事件或方法我应该补充。我该怎么办呢?谢谢你,在前进。
的解决方案
请自定义的的ListView
并重写
的MouseMove
(奖金 - <大骨节病>点击率骨节病> + <大骨节病> A 骨节病>支持): [System.ComponentModel.DesignerCategory(code)
公共类MyListView:ListView控件
{
保护覆盖无效的OnMouseMove(MouseEventArgs E)
{
//鼠标就可以轻松选择
如果(MouseButtons == MouseButtons.Left)
{
VAR项目= this.HitTest(e.Location).Item;
如果(项目!= NULL)
item.Selected = TRUE;
}
base.OnMouseMove(E);
} 保护覆盖无效的onkeydown(KeyEventArgs E)
{
// CTRL-A - 选择所有
如果(e.Key code == Keys.A&放大器;&安培; e.Control)
全选();
base.OnKeyDown(E);
}
}
这是不是最好的实现,如果移动太快,那么一些项目将被跳过。
的几点说明
的ListView
是有点特殊的控制,因为它有present数据不同的看法。在图标的查看您所选择的多个项目用鼠标绘制一个矩形的(preSS按钮,绘制矩形,释放)。在同一作品的详情的视图。全部由矩形相交的项目会被选中。重要提示:选择这种方式需要从绘制矩形起点按项目未占用区
问题:在的详情的查看位于最后一列的权利和最后一个项目的底部未占用区域。常列采取所有的横向空间,物品采取一切垂直的,所以有没有这样的区域存在。所以,选择这种方式是行不通的!
其他知名的可能性来选择的ListView
多个项目,是选择第一项,preSS <大骨节病>移骨节病>,然后点击最后项目。在(中的图标的视图或矩形)范围内的所有项目将被选中。或保持pssed <大骨节病>点击率骨节病> $ P $,然后单击项来切换选择他们逐个。
I need to add mouse multiselection support to a winform listview. (User will click one item and drag it to fifth and she'll be able to select items 1 to 5) I think I should inherit the listview to add new features to it but i don't know which events or methods should i add. How can i do it? Thanks, in advance.
Make custom ListView
of and override MouseMove
(bonus - Ctr+A support):
[System.ComponentModel.DesignerCategory("Code")]
public class MyListView : ListView
{
protected override void OnMouseMove(MouseEventArgs e)
{
// easy mouse selection
if (MouseButtons == MouseButtons.Left)
{
var item = this.HitTest(e.Location).Item;
if (item != null)
item.Selected = true;
}
base.OnMouseMove(e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
// ctrl-a - select all
if (e.KeyCode == Keys.A && e.Control)
SelectAll();
base.OnKeyDown(e);
}
}
It's not the best implementation, if you move too fast, then some items will be skipped.
Some explanations
ListView
is a bit special control, because it has different views to present data. In icon view you select multiple items by drawing a rectangle with mouse (press button, draw rectangle, release). Same works in details view. All intersected by rectangle items will be selected. Important: this way of selection required to draw rectangle starting from unoccupied by items area.
Problem: in details view unoccupied area located to the right of last column and to the bottom of last item. Often columns take all horizontal space, items take all vertical, so there is no such area exists then. So this way of selection will not work!
Other well known possibility to to select multiple items in ListView
is to select first item, press Shift and then click on last item. All items in the range (or rectangle in icon view) will be selected. Or keep Ctr pressed and click items to toggle their selection one by one.
这篇关于如何鼠标多选支持添加到列表视图WinForm的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!