C#移动列表框中的最后一个剩余项目

时间:2015-02-12 12:34:11

标签: c# listbox items

这是一个Windows窗体应用程序,它连接到服务器,获取表并将它们转换为drop create SQL脚本。有两个列表框,lbSource,其中加载了表的初始列表,lbTarget包含用户在那里移动以导出的表。

发生的问题是,如果我移动最后剩余的项目(selIndex = 0),则没有项目可供选择(lbTarget.SetSelected(selIndex, true);) selIndex获取值,但列表框为空(OutOfRange)。 我需要(selIndex >= 0)处的代码选择下一个项目,如果它位于列表(also selIndex = 0)的顶部。

所以我试图做的是检查是否(listBox.Items.Count == 0),但这似乎不起作用。

如果您单击按钮将(a)某些项目从一个列表框移动到另一个列表框,则会发生以下代码。源到目标的代码相同。

private void cmdTargetToSource_Click(object sender, EventArgs e)
{
    //move more than one item
    lbTarget.SelectionMode = SelectionMode.MultiSimple;

    //sorting the lists
    lbSource.Sorted = true;
    lbTarget.Sorted = true;

    //save the selectedIndex
    int selIndex = lbTarget.SelectedIndex;

    if (lbTarget.SelectedIndex == -1)
    {
        return;
    }

    //moving last entry in listBox sets selIndex higher than lbTarget.Items.Count
    if (selIndex < lbTarget.Items.Count)
    {
        selIndex = selIndex - 1;
    }

    if (selIndex == -1)
    {
        selIndex = selIndex + 1;
    }

    //If there are no items left do nothing
    if (lbTarget.Items.Count == 0)
    {
     return;
    }

    if (selIndex == lbTarget.Items.Count -2)
    {
        selIndex = selIndex - 1;
    }   

    MoveListBoxItems(lbTarget, lbSource);

    //select next item
    if (selIndex >= 0)
    {
        lbTarget.SetSelected(selIndex, true);
    }

    //selectionmode back to single selection
    lbTarget.SelectionMode = SelectionMode.One;
}

1 个答案:

答案 0 :(得分:0)

让我们看一下:点击cmdTargetToSource上的时,您希望将所有选定的项目ListBox lbSource移至ListBox lbTarget,对?   在这种情况下,首先提取方法

private static void MoveSelectedItems(ListBox source, ListBox target) {
  if (null == source)
    throw new ArgumentNullException("source");
  else if (null == target)
    throw new ArgumentNullException("target");

  // Indice to move (Linq)
  var indice = source.SelectedIndices.OfType<int>().OrderByDescending(item => item);
  // Place to move (at the end of target listbox, if target is not sorted)
  int place = target.Items.Count;

  // we don't need repaint source as well as target on moving each item
  // which cause blinking, so let's stop updating them for the entire operation
  source.BeginUpdate();
  target.BeginUpdate();
  Boolean sorted = target.Sorted;

  try {
    // switch sorting off so sorting would not change indice 
    // of items inserted
    target.Sorted = false;

    // Move items from source to target
    foreach (var index in indice) {
      target.Items.Insert(place, source.Items[index]);
      target.SelectedIndices.Add(place);

      source.Items.RemoveAt(index);
    }
  }
  finally {
    target.Sorted = true;
    target.EndUpdate();
    source.EndUpdate();
  }
}  

然后调用它

  private void cmdTargetToSource_Click(object sender, EventArgs e) {
    MoveSelectedItems(lbSource, lbTarget);
  }

提取方法如果需要,您可以轻松实现反向移动:

  private void cmdBackTargetToSource_Click(object sender, EventArgs e) {
    // Just swap source and target:
    MoveSelectedItems(lbTarget, lbSource);
  }