0

我有一个 CListBox,我想要一个上移/下移按钮,它可以向上或向下移动当前选定的项目。

现在我认为唯一的解决方案是删除该项目,然后将其插入新位置。

有没有更有效的方法来做到这一点?

4

3 回答 3

3

这是我 10 年前做的一个片段。它使用删除和添加来切换位置,但我认为这是唯一的方法。

void CKnoepfeDlg::OnDown() 
{
    int item = m_list.GetNextItem(-1,LVNI_SELECTED);
    if(item == -1) 
        return;

    if(item < m_list.GetItemCount() - 1)
    {
        CString name,befehl;
        name = m_list.GetItemText(item,0);
        befehl = m_list.GetItemText(item,1);
        m_list.DeleteItem(item);
        m_list.InsertItem(item + 1,name);
        m_list.SetItemText(item + 1,1,befehl);
        m_list.SetItemState(item + 1,LVNI_SELECTED,LVIS_SELECTED);
    }
}
于 2012-08-22T11:05:19.737 回答
0

以下代码适用于 Move Up。

   void CStreamTable::OnBnClickedMoveUp()
   {
      int item = m_InputStreamListControl.GetNextItem(-1, LVNI_SELECTED);
      if (item == -1)
        return;

      if (item > 0)
      {
          CString name, befehl;
          name = m_InputStreamListControl.GetItemText(item, 0);
          befehl = m_InputStreamListControl.GetItemText(item, 1);
          m_InputStreamListControl.DeleteItem(item);
          m_InputStreamListControl.InsertItem(item - 1, name);
          m_InputStreamListControl.SetItemText(item - 1, 1, befehl);
          m_InputStreamListControl.SetItemState(item - 1, LVNI_SELECTED, LVIS_SELECTED);
       }
    }
于 2015-08-19T10:29:18.420 回答
0

有点晚了,但这里有一个 MFC CListBox 的工作解决方案。
它支持多选并避免在选定项目到达顶部/底部时重新排序。


// --------------------------------------------------------------- //
// Move item with index 'item' one place up
// --------------------------------------------------------------- //
void CListBox_MoveItemUp(CListBox& box, int item) {
    if(item <= 0) { return; }
    CString oldstring;
    box.GetText(item, oldstring);
    DWORD_PTR olddata = box.GetItemData(item);
    int oldsel = box.GetSel(item);

    box.DeleteString(item);
    item = box.InsertString(item-1, oldstring);
    box.SetItemData(item, olddata);
    box.SetSel(item, oldsel);
}

// --------------------------------------------------------------- //
// Move all items one place up, if there is space to move
// --------------------------------------------------------------- //
void CListBox_MoveSelectedItemsUp(CListBox& box, std::vector<INT> sels) {
    std::sort(sels.begin(), sels.end());

    // if 0 is selected, it might block all next selected items, too.
    for(int i=0; !sels.empty(); ++i) {
        if(sels.front() == i) {
            sels.erase(sels.begin());
        } else {
            break;
        }
    }

    for(int isel = 0; isel < int(sels.size()); ++isel) {
        int item = int(sels[isel]);
        CListBox_MoveItemUp(box, item);
    }
}

// --------------------------------------------------------------- //
// Move item with index 'item' one place down
// --------------------------------------------------------------- //
void CListBox_MoveItemDown(CListBox& box, int item) {
    if(item+1 >= box.GetCount()) { return; }
    CString oldstring;
    box.GetText(item, oldstring);
    DWORD_PTR olddata = box.GetItemData(item);
    int oldsel = box.GetSel(item);

    box.DeleteString(item);
    item = box.InsertString(item+1, oldstring);
    box.SetItemData(item, olddata);
    box.SetSel(item, oldsel);
}

// --------------------------------------------------------------- //
// Move all items one place down, if there is space to move
// --------------------------------------------------------------- //
void CListBox_MoveSelectedItemsDown(CListBox& box, std::vector<INT> sels) {
    std::sort(sels.begin(), sels.end());

    // if last is selected, it might block all previous selected items, too.
    for(int i=box.GetCount()-1; !sels.empty(); --i) {
        if(sels.back() == i) {
            sels.pop_back();
        } else {
            break;
        }
    }

    for(int isel = int(sels.size())-1; isel>=0; --isel) {
        int item = int(sels[isel]);
        CListBox_MoveItemDown(box, item);
    }
}


//---------------------------------------------------------------//
// Move selected items up in the listbox
//---------------------------------------------------------------//
void MyDialog::OnBnClickedMoveUp() {
    int nsel = m_MyListBox.GetSelCount();
    if(!nsel) { return; }
    std::vector<INT> selectedItems;
    selectedItems.resize(nsel);
    m_MyListBox.GetSelItems(nsel, &selectedItems[0]);
    CListBox_MoveSelectedItemsUp(m_MyListBox, selectedItems);
}


//---------------------------------------------------------------//
// Move selected items down in the listbox
//---------------------------------------------------------------//
void MyDialog::OnBnClickedMoveDown() {
    int nsel = m_MyListBox.GetSelCount();
    if(!nsel) { return; }
    std::vector<INT> selectedItems;
    selectedItems.resize(nsel);
    m_MyListBox.GetSelItems(nsel, &selectedItems[0]);
    CListBox_MoveSelectedItemsDown(m_MyListBox, selectedItems);
}
于 2021-04-21T14:39:21.493 回答