簡體   English   中英

C#在列表框中移動最后剩余的項目

[英]C# moving the last remaining item in a list box

這是一個連接到服務器,獲取表並將其轉換為拖放創建SQL腳本的Windows窗體應用程序。 有兩個列表框,lbSource用於加載表的初始列表,lbTarget包含用戶移至此處以導出的表。

發生的問題是,如果我移動了最后一個剩余的項目(selIndex = 0) ,則沒有剩余的項目(selIndex = 0)選擇(lbTarget.SetSelected(selIndex, true);) selIndex得到一個值,但是列表框為空(OutOfRange )。 我需要位於(selIndex >= 0)的代碼來選擇下一個項目(如果它是列表的頂部) (also selIndex = 0)

所以我試圖做的是檢查(listBox.Items.Count == 0) ,但這似乎不起作用。

這是當您單擊按鈕將某些項目從一個列表框移動到另一個列表時發生的代碼。 源到目標的代碼相同。

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;
}

讓我們看看:當您單擊 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);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM