C#-如何在列表框(Windows窗体)中上下移动多个项目

C#-如何在列表框(Windows窗体)中上下移动多个项目,c#,winforms,listbox,C#,Winforms,Listbox,我在Windows窗体应用程序中有一个列表框。 我可以使用以下脚本轻松地在列表框中上下移动单个项目: int newIndex = inputFiles.SelectedIndex + direction; if (newIndex < 0) newIndex = inputFiles.Items.Count-1; if (newIndex >= inputFiles.Items.Count) newIndex = 0; object selected = inp

我在Windows窗体应用程序中有一个列表框。 我可以使用以下脚本轻松地在列表框中上下移动单个项目:

int newIndex = inputFiles.SelectedIndex + direction;

if (newIndex < 0)
   newIndex = inputFiles.Items.Count-1;

if (newIndex >= inputFiles.Items.Count)
    newIndex = 0;

object selected = inputFiles.SelectedItem;

inputFiles.Items.Remove(selected);
inputFiles.Items.Insert(newIndex, selected);
inputFiles.SetSelected(newIndex, true);
int newIndex=inputFiles.SelectedIndex+方向;
如果(新索引<0)
newIndex=inputFiles.Items.Count-1;
if(newIndex>=inputFiles.Items.Count)
newIndex=0;
所选对象=inputFiles.SelectedItem;
inputFiles.Items.Remove(选中);
inputFiles.Items.Insert(newIndex,选中);
inputFiles.SetSelected(newIndex,true);

如何移动多个选定项目?谢谢大家!

我认为没有一种特定的方法可以在列表框中移动列表。不过,有一个AddRange()将其全部设置为列表的底部

您可以尝试制作自己的InsertRange(),类似这样的内容

List<object> toInsert = new List<object>();
toInsert.Add(selected);


InsertRange(int startIndex){
     foreach(object o in toInsert){
          inputFiles.Items.Insert(startIndex, o);
          startIndex++;
     }
}
List toInsert=newlist();
toInsert.Add(已选择);
插入范围(int startIndex){
foreach(toInsert中的对象o){
inputFiles.Items.Insert(startIndex,o);
startIndex++;
}
}

这可能并不完全有效,但我认为这可能是您的要求。

如果您将所选索引复制到一个数组中,您可以循环遍历项目并适当地更新索引:

private void btnDown_Click(object sender, EventArgs e) {
  listBox1.BeginUpdate();
  int[] indexes = listBox1.SelectedIndices.Cast<int>().ToArray();
  if (indexes.Length > 0 && indexes[indexes.Length - 1] < listBox1.Items.Count - 1) {
    for (int i = listBox1.Items.Count - 1; i > -1; --i) {
      if (indexes.Contains(i)) {
        object moveItem = listBox1.Items[i];
        listBox1.Items.Remove(moveItem);
        listBox1.Items.Insert(i + 1, moveItem);
        listBox1.SetSelected(i + 1, true);
      }
    }
  }
  listBox1.EndUpdate();
}

private void btnUp_Click(object sender, EventArgs e) {
  listBox1.BeginUpdate();
  int[] indexes = listBox1.SelectedIndices.Cast<int>().ToArray();
  if (indexes.Length > 0 && indexes[0] > 0) {
    for (int i = 0; i < listBox1.Items.Count; ++i) {
      if (indexes.Contains(i)) {
        object moveItem = listBox1.Items[i];
        listBox1.Items.Remove(moveItem);
        listBox1.Items.Insert(i - 1, moveItem);
        listBox1.SetSelected(i - 1, true);
      }
    }
  }
  listBox1.EndUpdate();
}
private void btnDown\u单击(对象发送者,事件参数e){
listBox1.BeginUpdate();
int[]index=listBox1.SelectedIndices.Cast().ToArray();
if(index.Length>0&&index[index.Length-1]-1;--i){
if(索引包含(i)){
object moveItem=listBox1.Items[i];
listBox1.Items.Remove(移动项);
listBox1.Items.Insert(i+1,moveItem);
listBox1.SetSelected(i+1,true);
}
}
}
listBox1.EndUpdate();
}
私有无效btnUp_单击(对象发送方,事件参数e){
listBox1.BeginUpdate();
int[]index=listBox1.SelectedIndices.Cast().ToArray();
if(index.Length>0&&index[0]>0){
对于(int i=0;i
这对我很有用

/// <summary>
/// Extension allow move with multiple selected items 
/// </summary>
static class ListBoxExtension {
    /// <summary>
    /// Moves the selected items up one level
    /// </summary>
    /// <param name="xListBox"></param>
    public static void MoveUp(ListBox xListBox) {
        if(xListBox.SelectedItems.Count > 0) {
            if(xListBox.SelectedIndices[0] > 0) {
                foreach(int i in xListBox.SelectedIndices) {
                    object NewItem = xListBox.Items[i];
                    xListBox.Items.Remove(NewItem);
                    xListBox.Items.Insert(i - 1, NewItem);
                    xListBox.SelectedItem = NewItem;
                }
            }
        }
    }

    /// <summary>
    /// Moves the selected items one level down
    /// </summary>
    /// <param name="xListBox"></param>
    public static void MoveDown(ListBox xListBox) {
        if(xListBox.SelectedItems.Count > 0) {
            int LastSelectedIndex = xListBox.SelectedIndices[xListBox.SelectedIndices.Count - 1];
            int LastIndex = xListBox.Items.Count - 1;

            if(LastSelectedIndex < LastIndex) {
                foreach(int i in xListBox.SelectedIndices) {
                    object NewItem = xListBox.Items[i];
                    xListBox.Items.Remove(NewItem);
                    xListBox.Items.Insert(i + 1, NewItem);
                    xListBox.SelectedItem = NewItem;
                }
            }
        }
    }
}
//
///扩展允许移动多个选定项目
/// 
静态类ListBoxExtension{
/// 
///将所选项目上移一级
/// 
/// 
公共静态无效移动(列表框xListBox){
如果(xListBox.SelectedItems.Count>0){
如果(xListBox.selectedDices[0]>0){
foreach(xListBox中的int i。选择的骰子){
objectnewitem=xListBox.Items[i];
xListBox.Items.Remove(NewItem);
xListBox.Items.Insert(i-1,NewItem);
xListBox.SelectedItem=NewItem;
}
}
}
}
/// 
///将选定项目下移一级
/// 
/// 
公共静态无效下移(列表框xListBox){
如果(xListBox.SelectedItems.Count>0){
int LastSelectedIndex=xListBox.SelectedDices[xListBox.SelectedDices.Count-1];
int LastIndex=xListBox.Items.Count-1;
如果(LastSelectedIndex
在循环中执行同样的操作?请为您的精彩代码添加一个简短的解释