在项目上拖动时在列表视图上创建自动滚动
Create auto scroll on listview when dragging over an item
如何在 listview dragOver 上创建自动滚动。
我是按照以下方式做的:
private void groupsCharacterListView_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(List<ListViewItem>)))
{
groupsCharacterListView.DragOver += RevealMoreItems;
groupsCharacterListView.DragOver += (source, e) =>
{
e.Effect = DragDropEffects.Move;
};
}
}
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;
listView.PointToScreen(new Point(e.X, e.Y + 40));
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();
}
}
但它给了我以下错误:
A local or parameter named 'e' cannot be declared in this scope
because that is used in an enclosing local scope to define a local or
parameter.
错误描述的很清楚。您不能在此行使用 e
,因为它已在您的事件处理程序中使用。
这里是事件处理程序:
private static void RevealMoreItems(object sender, DragEventArgs e)
这里的代码:
var point = listView.PointToClient(new Point(e.X, e.Y));
listView.PointToScreen(new Point(e.X, e.Y + 40));
尝试使用其他变量名。
如何在 listview dragOver 上创建自动滚动。
我是按照以下方式做的:
private void groupsCharacterListView_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(List<ListViewItem>)))
{
groupsCharacterListView.DragOver += RevealMoreItems;
groupsCharacterListView.DragOver += (source, e) =>
{
e.Effect = DragDropEffects.Move;
};
}
}
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;
listView.PointToScreen(new Point(e.X, e.Y + 40));
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();
}
}
但它给了我以下错误:
A local or parameter named 'e' cannot be declared in this scope because that is used in an enclosing local scope to define a local or parameter.
错误描述的很清楚。您不能在此行使用 e
,因为它已在您的事件处理程序中使用。
这里是事件处理程序:
private static void RevealMoreItems(object sender, DragEventArgs e)
这里的代码:
var point = listView.PointToClient(new Point(e.X, e.Y));
listView.PointToScreen(new Point(e.X, e.Y + 40));
尝试使用其他变量名。