Copying/Moving Rows in CListCtrl

This article is very strightfoward but I thought it was a feature that most people could use in their own custom CListCtrls or CLIstViews.

I had the need in one of my projects to allow the user to rearrange the order of the ListCtrl but I didn't want to allow them to drag and drop. Therefore I came up with this.


BOOL CListCtrlEx::MoveRow(int from, int to)
{
	//Can't move to the same place, or from or to a negative index
	if(from == to || from < 0 || to < 0)
		return FALSE;

	//First Copy the row to the new location
	if(CopyRow(from, to))
	{
		//If we have just inserted a row before
		//this one in the list, we need to increment
		//our index.
		if(from > to)
			DeleteItem(from + 1);
		else
			DeleteItem(from);

		return TRUE;
	}
	else
		return FALSE;
}

BOOL CListCtrlEx::CopyRow(int from, int to)
{	
	//Can't move to the same place, or from or to a negative index
	if(from == to || from < 0 || to < 0)
		return FALSE;

	//Copy the row to the new index
	InsertItem(to, GetItemText(from, 0));

	//If row has been inserted before original
	//increment the original
	if(from > to)
		from++;

	//Loop through subitems
	for(int i = 1; i < GetColumnCount(); i++)
	{
		SetItemText(to, i, GetItemText(from, i));
	}

	return TRUE;
}

Remember that if you want to move a row one row upwards in the list you can use:


m_List.MoveRow(index, index - 1);

But to move it one row down you need:


m_List.MoveRow(index, index + 2);

This is because if you just enter 'index + 1' as the destination, the new row is inserted immeadiately after the current one. The current one is then deleted so you end up in the same place!

Ahh... Trial and Error is a wonderful thing.

History



Comments

  • More to item than text and state

    Posted by magnesium on 03/31/2006 04:52am

    Keep in mind though that there's more to an item than text and state. The item data (lParam) for example, is a very important member in most cases and should not be forgotten. To avoid forgetting anything important, you may want to use GetItem() and SetItem() instead, modify LVITEM structure to ignore the iItem member, or change accordingly.

    Reply
  • Nice

    Posted by Legacy on 01/29/2004 12:00am

    Originally posted by: Jibesh

    Woooooow.. Great.. Nice work
    
    

    thx

    Jibesh

    Reply
  • A wonderful thing

    Posted by Legacy on 09/14/1999 12:00am

    Originally posted by: Julia Child

    I didn't know that Martha Stewart is a programmer.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 6, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds