C#实现ListView选中项向上或向下移动的方法

本文实例讲述了C#实现ListView选中项向上或向下移动的方法。分享给大家供大家参考。具体实现方法如下:

private void buttonUp_Click(object sender, EventArgs e) 
{ 
  if (listView.SelectedItems.Count > 0 && listView.SelectedItems[0].Index != 0) 
  { 
    listView.BeginUpdate(); 
    foreach (ListViewItem lvi in listView.SelectedItems) 
    { 
      ListViewItem item = lvi; 
      int index = lvi.Index; 
      listView.Items.RemoveAt(index); 
      listView.Items.Insert(index - 1, item); 
    } 
    listView.EndUpdate(); 
  } 
  listView.Focus(); 
} 
private void buttonDown_Click(object sender, EventArgs e) 
{ 
  if (listView.SelectedItems.Count > 0 && listView.SelectedItems[listView.SelectedItems.Count - 1].Index < (listView.Items.Count-1)) 
  { 
    listView.BeginUpdate(); 
    int count = listView.SelectedItems.Count; 
    foreach (ListViewItem lvi in listView.SelectedItems) 
    { 
      ListViewItem item = lvi; 
      int index = lvi.Index; 
      listView.Items.RemoveAt(index); 
      listView.Items.Insert(index + count, item); 
    } 
    listView.EndUpdate(); 
  } 
  listView.Focus(); 
}

希望本文所述对大家的C#程序设计有所帮助。

你可能感兴趣的:(C#实现ListView选中项向上或向下移动的方法)