本帖最后由 Bran 于 2016-2-7 18:27 编辑
ListView选中selectedItem进行上下移动
//上移
[C#] 纯文本查看 复制代码 private void ListViewUpMove(ListView listView)
{
if (listView.SelectedItems.Count == 0)
{
return;
}
listView.BeginUpdate();
if (listView.SelectedItems[0].Index > 0)
{
foreach (ListViewItem lvi in listView.SelectedItems)
{
ListViewItem lviSelectedItem = lvi;
int indexSelectedItem = lvi.Index;
listView.Items.RemoveAt(indexSelectedItem);
listView.Items.Insert(indexSelectedItem - 1, lviSelectedItem);
}
}
listView.EndUpdate();
if (listView.Items.Count > 0 && listView.SelectedItems.Count > 0)
{
listView.Focus();
listView.SelectedItems[0].Focused = true;
listView.SelectedItems[0].EnsureVisible();
}
}
//下移
[C#] 纯文本查看 复制代码 private void ListViewDownMove(ListView listView)
{
if (listView.SelectedItems.Count == 0)
{
return;
}
listView.BeginUpdate();
int indexMaxSelectedItem = listView.SelectedItems[listView.SelectedItems.Count - 1].Index;
if (indexMaxSelectedItem < listView.Items.Count - 1)
{
for (int i = listView.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem lviSelectedItem = listView.SelectedItems[i];
int indexSelectedItem = lviSelectedItem.Index;
listView.Items.RemoveAt(indexSelectedItem);
listView.Items.Insert(indexSelectedItem + 1, lviSelectedItem);
}
}
listView.EndUpdate();
if (listView.Items.Count > 0 && listView.SelectedItems.Count > 0)
{
listView.Focus();
listView.SelectedItems[listView.SelectedItems.Count - 1].Focused = true;
listView.SelectedItems[listView.SelectedItems.Count - 1].EnsureVisible();
}
}
|