C#在拖放时在ListView中实现自动滚动删除 [英] C# Implementing Auto-Scroll in a ListView while Drag & Dropping
问题描述
如何在Winforms ListView中实现自动滚动(例如,当您靠近顶部或底部时ListView滚动)?我几乎没有运气在谷歌上狩猎过。我不敢相信这不行的开箱即用!
提前感谢
Dave
How do I implement Auto-Scrolling (e.g. the ListView scrolls when you near the top or bottom) in a Winforms ListView? I've hunted around on google with little luck. I can't believe this doesn't work out of the box! Thanks in advance Dave
推荐答案
滚动可以通过 ListViewItem.EnsureVisible 方法。
您需要确定您当前拖动的项目是否在列表视图的可见边界,而不是第一个/最后一个。
Scrolling can be accomplished with the ListViewItem.EnsureVisible method. You need to determine if the item you are currently dragging over is at the visible boundary of the list view and is not the first/last.
private static void RevealMoreItems(object sender, DragEventArgs e)
{
var listView = (ListView)sender;
var point = listView.PointToClient(new Point(e.X, e.Y));
var item = listView.GetItemAt(point.X, point.Y);
if (item == null)
return;
var index = item.Index;
var maxIndex = listView.Items.Count;
var scrollZoneHeight = listView.Font.Height;
if (index > 0 && point.Y < scrollZoneHeight)
{
listView.Items[index - 1].EnsureVisible();
}
else if (index < maxIndex && point.Y > listView.Height - scrollZoneHeight)
{
listView.Items[index + 1].EnsureVisible();
}
}
然后将此方法连接到DragOver事件。 p>
Then wire this method to the DragOver event.
targetListView.DragOver += RevealMoreItems;
targetListView.DragOver += (sender, e) =>
{
e.Effect = DragDropEffects.Move;
};
这篇关于C#在拖放时在ListView中实现自动滚动删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!