可以在ListBox中同时移动列表中的多个项目,但不能停止?

时间:2015-07-23 21:45:40

标签: c# .net winforms

我有一个奇怪的错误,我很难搞清楚。

如果我在ListBox中选择多个项目,然后选择向上箭头,则所有项目都会完美地向上移动一个空格,就像它们是一个单元一样。但是,单击向下箭头将项目向下移动到列表中的每个位置时,不会发生这种情况。

相反,如果我选择偶数个项目然后单击向下箭头,则根本没有任何项目移动,如果我选择奇数个项目然后单击向下箭头,则只有底部项目向下移动一个空间,而所有其他人都留在原地。

任何人都知道为什么会发生这种情况?

以下是我用来处理项目移动的代码:

private void MoveItemInSelectedItemsListBox(object sender)
{
  if (selectedItemsListBox.SelectedItems.Count > 0)
  {
    // Build list of items to be moved
    List<object> listOfItemsToBeMoved = new List<object>();

    for (int i = 0; i < selectedItemsListBox.SelectedItems.Count; i++)
    {
      listOfItemsToBeMoved.Add(selectedItemsListBox.SelectedItems[i]);
    }

    for (int k = 0; k < listOfItemsToBeMoved.Count; k++)
    {
      var selectedItem = listOfItemsToBeMoved[k];
      int selectedItemIndex = selectedItemsListBox.Items.IndexOf(selectedItem);
      int numberOfItemsInList = selectedItemsListBox.Items.Count;

      // If up arrow was clicked
      if (sender.ToString().Contains("▲"))
      {
        if (selectedItemIndex == 0)
        {
          selectedItemsListBox.Items.Remove(selectedItem);
          selectedItemsListBox.Items.Insert(numberOfItemsInList - 1, selectedItem);
          selectedItemsListBox.SetSelected(numberOfItemsInList - 1, true);
        }
        else
        {
          selectedItemsListBox.Items.Remove(selectedItem);
          selectedItemsListBox.Items.Insert(selectedItemIndex - 1, selectedItem);
          selectedItemsListBox.SetSelected(selectedItemIndex - 1, true);
        }
      }
      // If down arrow was clicked
      else if (sender.ToString().Contains("▼"))
      {
        if (selectedItemIndex == numberOfItemsInList - 1)
        {
          selectedItemsListBox.Items.Remove(selectedItem);
          selectedItemsListBox.Items.Insert(0, selectedItem);
          selectedItemsListBox.SetSelected(0, true);
        }
        else
        {
          selectedItemsListBox.Items.Remove(selectedItem);
          selectedItemsListBox.Items.Insert(selectedItemIndex + 1, selectedItem);
          selectedItemsListBox.SetSelected(selectedItemIndex + 1, true);
        }
      }
    }
  }

}

2 个答案:

答案 0 :(得分:2)

for循环的方向存在问题:实质上,当向下移动项目时,如果您实际上正在通过列表移动向上,同时检查项目移动向下,最终会出现重叠和其他奇怪的事情。我的解决方案就是如果您要移动项目,只需要循环下去项目:

if (selectedItemsListBox.SelectedItems.Count > 0)
{
    List<object> itemsToMove = (from object item in selectedItemsListBox.SelectedItems select item).ToList();
    int numItems = selectedItemsListBox.Items.Count;

    // If up arrow was clicked
    if (sender.ToString().Contains("▲"))
    {
        for (int i = 0; i < itemsToMove.Count; i++)
        {
            var selectedItem = itemsToMove[i];
            int oldIndex = selectedItemsListBox.Items.IndexOf(selectedItem);
            int newIndex = oldIndex == 0 ? numItems - 1 : oldIndex - 1;

            selectedItemsListBox.Items.Remove(selectedItem);
            selectedItemsListBox.Items.Insert(newIndex, selectedItem);
            selectedItemsListBox.SetSelected(newIndex, true);
        }
    }
    // If down arrow was clicked
    else if (sender.ToString().Contains("▼"))
    {
        for (int i = itemsToMove.Count - 1; i >= 0; i--)
        {
            var selectedItem = itemsToMove[i];
            int oldIndex = selectedItemsListBox.Items.IndexOf(selectedItem);
            int newIndex = oldIndex == numItems - 1 ? 0 : oldIndex + 1;

            selectedItemsListBox.Items.Remove(selectedItem);
            selectedItemsListBox.Items.Insert(newIndex, selectedItem);
            selectedItemsListBox.SetSelected(newIndex, true);
        }
    }
}

(我也冒昧地将一些代码缩小,例如通过更改一些变量名称。)

答案 1 :(得分:0)

发现这个example可能是一个更好,更清晰和可重复使用的选项。

public class SmartListBox : ListBox
{
    //Moves the selected items up one level
    public MoveUp()
    {

        for(int i = 0; i < Items.Count; i++)
        {
            if (Items[i].Selected)//identify the selected item
            {
                //swap with the top item(move up)
                if (i > 0 && !Items[i - 1].Selected)
                {
                     ListItem bottom = Items[i];
                     Items.Remove(bottom);
                     Items.Insert(i - 1, bottom);
                     Items[i - 1].Selected = true;
                 }
              }
          }
     }

     //Moves the selected items one level down
     public MoveDown()
     {
         int startindex = Items.Count -1;
         for (int i = startindex; i > -1; i--)
         {
              if (Items[i].Selected)//identify the selected item
              { 
                  //swap with the lower item(move down)
                  if (i < startindex && !Items[i + 1].Selected)
                  {
                       ListItem bottom = Items[i];
                       Items.Remove(bottom);
                       Items.Insert(i + 1, bottom);
                       Items[i + 1].Selected = true;
                  }

              }
         }
     }
}