C#在ListView中实现拖拽时自动滚动&掉落

C# Implementing Auto-Scroll in a ListView while Drag amp; Dropping(C#在ListView中实现拖拽时自动滚动amp;掉落)
本文介绍了C#在ListView中实现拖拽时自动滚动&掉落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在 Winforms ListView 中实现自动滚动(例如,当您靠近顶部或底部时,ListView 会滚动)?我在谷歌上四处搜寻,运气不佳.我不敢相信这不是开箱即用的!提前致谢戴夫

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 事件.

Then wire this method to the DragOver event.

targetListView.DragOver += RevealMoreItems;

targetListView.DragOver += (sender, e) =>
{
    e.Effect = DragDropEffects.Move;
};

这篇关于C#在ListView中实现拖拽时自动滚动&amp;掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)