Search code examples
c++windowswxwidgets

How to move an item up and down in a wxListCtrl (wxwidgets)


This should be pretty easy but I'm having a heck of a time doing it. Basically I want to move a row in my wxListCtrl up or down. I posted this to wxwidgets forum and got the following code.

m_list->Freeze(); 
wxListItem item; 
item.SetId(item_id); // the one which is selected 
m_list->GetItem(item); // Retrieve the item 
m_list->DeleteItem(item_id); // Remove it 
item.SetId(item_id - 1); // Move it up 
m_list->SetItem(item); // Apply it's new pos in the list 
m_list->Thaw();

which doesn't work. The element is deleted but not moved up (I guess the setitem line is not working). Then I thought to just switch the text and the image but I can't even get the text from the row reliably. I have

int index = m_right->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
wxString label = m_right->GetItemText(index);

if(index == 0)
  return;

wxListItem item; 
item.SetId(index); 
bool success = m_right->GetItem(item); 
wxString text = item.GetText();

but text is blank even though there is text and the index is correct. So, I'm stuck not even being able to do the most basic task. Anybody know how to do this? The code runs in a button callback (the user presses a little up arrow and my code executes to try to move it). I'm using 2.9.1 on windows.


Solution

  • Is the list ordered? if it is auto ordering it may be ignoring the order you are trying to apply.

    From recollection the internal order was not necessarily sequential, you might have to get the index of the previous item and go one before it.