Click here to Skip to main content
15,895,827 members
Articles / Programming Languages / C++

Dialog to select and sort data with CListBox

Rate me:
Please Sign up or sign in to vote.
4.60/5 (2 votes)
17 Oct 2013CPOL1 min read 20.8K   419   8  
Dialog box to choose data / Items and sort them
/////////////////////////////////////////////////////////
//Fonctions diverses et utiles
//
//by MaxMax14 13/10/13
//
///////////////////////////////////////////////////////////
//Function_1D.cpp

#pragma once

#include "stdafx.h"

#include "Function_1D.h"	

//-----------------------------------------------------------------------
BOOL Func_1::FillListBox(CListBox* pListBox, CString sListItems, CString sSep /* = "," */ )	//Rempli un CListBox (supprimer les �l�ments et recr�e tout) avec donn�es du CArray2D (colonnes d�finies dans sListColListCtrl)
{
	//pListBox			= CListBox � remplir
	//sListItems		= CString qui contient les donn�es
	//sSep				= S�parateur

	CString sNbLigne = "";
	int iNbLigne = 0;
	CString resToken = "";
	int curPos = 0;

	pListBox->ResetContent();

	resToken= sListItems.Tokenize(sSep,curPos);
	while (resToken != _T(""))
	{
		sNbLigne.Format("%i", iNbLigne);	//Convert int to CString 
		TRACE0("Ligne n�: " + sNbLigne + " : Token: " + resToken + "\n" );
		pListBox->AddString( resToken );				//si CListCtrlEx 
		resToken= sListItems.Tokenize(sSep,curPos);
		iNbLigne++;
	}

	return TRUE;
}

//-----------------------------------------------------------------------
CString Func_1::GetListBox(CListBox* pListBox, CString sSep /* = "," */)	//Renvoi liste des �l�ments du CListBox
{
	//pListBox			= CListBox � remplir
	//sSep				= S�parateur

	CString sListItems = "";
	CString sItems = "";
	int nCount = pListBox->GetCount();

	for(int i = 0; i < nCount; i++)
	{
		pListBox->GetText(i, sItems); 
		if ( sListItems == "")
		{
			sListItems = sItems;
		}
		else
		{
			sListItems = sListItems + sSep + sItems;
		}
	}
	return sListItems;
}

//-----------------------------------------------------------------------
BOOL Func_1::ListBox_UpItem( CListBox* pListBox )	//Remonte d'une ligne l'�l�ment s�lectionn� : http://www.codeproject.com/Tips/654882/Moving-listbox-items-up-and-down?cmt=512946#cmt2_654882
{
	int iPos = 0;
	int iLigne0 = -1;
	int iLigne1 = 0;
	int iLigne2 = 0;
	CString sItemsM = "";
	CString sItems1 = "";
	CString sItems2 = "";

	int nCount = pListBox->GetCount();

	int nSelCount = pListBox->GetSelCount();	//http://msdn.microsoft.com/en-us/library/fycfcb17%28v=vs.90%29.aspx
//DEBUT Simple s�lection
	if ( nSelCount == LB_ERR )	//If the list box is a single-selection list box, the return value is LB_ERR (-1).
	{
		int iPos = pListBox->GetCurSel();

		if ( iPos != 0 )
		{
			iLigne1 = iPos - 1;
			iLigne2= iPos;

			pListBox->GetText(iLigne1, sItems1); 
			pListBox->GetText(iLigne2, sItems2);
			pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
		}
		else
		{
			iLigne1 = 0;
			iLigne2= nCount - 1;

			pListBox->GetText(iLigne2, sItems2);

			pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne1 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx

			pListBox->InsertString(iLigne2, sItemsM);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
		}
		return TRUE;
	}
//FIN Simple s�lection

//DEBUT Get the indexes of all the selected items in aryListBoxSel
	CArray<int,int> aryListBoxSel;
	aryListBoxSel.SetSize(nSelCount);
	pListBox->GetSelItems(nSelCount, aryListBoxSel.GetData()); 
	AFXDUMP(aryListBoxSel);	// Dump the selection array.
//FIN  Get the indexes of all the selected items in aryListBoxSel

//DEBUT Multi-S�lection
	for(int i = 0; i < nSelCount; i++)
	{
		iPos = aryListBoxSel.GetAt(i);

		if ( iPos != 0 )
		{
			iLigne1 = iPos - 1;
			iLigne2= iPos;

			pListBox->GetText(iLigne1, sItems1);
			pListBox->GetText(iLigne2, sItems2);

			//DWORD dwItemData = pListBox->GetItemData(iLigne1);	//.dan.g
			int nIndex = pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			//pListBox->SetItemData(nIndex, dwItemData);	//.dan.g
			//pListBox->DeleteItem(...);					//.dan.g ?????

			pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
			if ( iPos == (iLigne0 + 1 ) )
			{
				iLigne0++;
			}
		}
		else
		{
			iLigne0 = 0;
			pListBox->GetText(iLigne0, sItemsM);
		}
	}

	if ( iLigne0 != -1)
	{
		iLigne1 = iLigne0;
		iLigne2= nCount - 1;

		pListBox->GetText(iLigne2, sItems2);

		pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
		pListBox->DeleteString(iLigne1 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx

		pListBox->InsertString(iLigne2, sItemsM);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
		pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
	}
//FIN Multi-S�lection

//DEBUT Re-S�lection des items
	for(int i = 0; i < nCount; i++)
	{
		pListBox->SetSel( i, FALSE);
	}
	for(int i = 0; i < nSelCount; i++)
	{
		iPos = aryListBoxSel.GetAt(i);
		if ( iPos != 0 )
		{
			pListBox->SetSel( iPos -1, TRUE);
		}
		else
		{
			pListBox->SetSel( nCount-1, TRUE);
		}
	}
//FIN Re-S�lection des items
	return TRUE;
}

//-----------------------------------------------------------------------
BOOL Func_1::ListBox_DownItem( CListBox* pListBox  )	//Descend d'une ligne l'�l�ment s�lectionn� : http://www.codeproject.com/Tips/654882/Moving-listbox-items-up-and-down?cmt=512946#cmt2_654882
{
	int iPos = 0;
	int iLigne0 = -1;
	int iLigne1 = 0;
	int iLigne2 = 0;
	CString sItemsM = "";
	CString sItems1 = "";
	CString sItems2 = "";

	int nCount = pListBox->GetCount();

	int nSelCount = pListBox->GetSelCount();	//http://msdn.microsoft.com/en-us/library/fycfcb17%28v=vs.90%29.aspx
//DEBUT Simple s�lection
	if ( nSelCount == LB_ERR )	//If the list box is a single-selection list box, the return value is LB_ERR (-1).
	{
		int iPos = pListBox->GetCurSel();

		if ( iPos != (nCount - 1) )
		{
			iLigne1 = iPos;
			iLigne2= iPos + 1;

			pListBox->GetText(iLigne1, sItems1); 
			pListBox->GetText(iLigne2, sItems2);
			pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
		}
		else
		{
			iLigne1 = 0;
			iLigne2= nCount - 1;

			pListBox->GetText(iLigne2, sItems2);

			pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne1 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx

			pListBox->InsertString(iLigne2, sItemsM);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
		}
		return TRUE;
	}
//FIN Simple s�lection

//DEBUT Get the indexes of all the selected items in aryListBoxSel
	CArray<int,int> aryListBoxSel;
	aryListBoxSel.SetSize(nSelCount);
	pListBox->GetSelItems(nSelCount, aryListBoxSel.GetData()); 
	AFXDUMP(aryListBoxSel);	// Dump the selection array.
//FIN  Get the indexes of all the selected items in aryListBoxSel

//DEBUT Multi-S�lection
	for(int i = (nSelCount-1); i >=0; i--)
	{
		iPos = aryListBoxSel.GetAt(i);

		if ( iPos != (nCount - 1) )
		{
			iLigne1 = iPos;
			iLigne2= iPos + 1;

			pListBox->GetText(iLigne1, sItems1); 
			pListBox->GetText(iLigne2, sItems2);
			pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
			pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
			if ( iPos == (iLigne0 - 1) )
			{
				iLigne0--;
			}
		}
		else
		{
			iLigne0 = nCount - 1;
			pListBox->GetText(iLigne0, sItemsM);
		}
	}

	if ( iLigne0 != -1)
	{
		iLigne1 = iLigne0;
		iLigne2 = 0;

		pListBox->GetText(iLigne2, sItems2);

		pListBox->InsertString(iLigne1, sItems2);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
		pListBox->DeleteString(iLigne1 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx

		pListBox->InsertString(iLigne2, sItemsM);	//InsertString: http://msdn.microsoft.com/en-us/library/80ay18e5%28v=vs.90%29.aspx
		pListBox->DeleteString(iLigne2 + 1);		//DeleteString: http://msdn.microsoft.com/en-us/library/466e49kb%28v=vs.90%29.aspx
	}
//FIN Multi-S�lection

//DEBUT Re-S�lection des items
	for(int i = 0; i < nCount; i++)
	{
		pListBox->SetSel( i, FALSE);
	}
	for(int i = (nSelCount-1); i >=0; i--)
	{
		iPos = aryListBoxSel.GetAt(i);
		if ( iPos != (nCount - 1) )
		{
			pListBox->SetSel( iPos + 1, TRUE);
		}
		else
		{
			pListBox->SetSel( 0, TRUE);
		}
	}
//FIN Re-S�lection des items
	return TRUE;
}

//-----------------------------------------------------------------------
void Func_1::GetListBox_Selected( CListBox* pListBox, CArray<int,int> &aArray)	//Renvoi dans aArray les �l�ments s�lectionn�s
{
	if ( aArray.GetSize() > 0 )
	{
		aArray.RemoveAll();
	}

	int iPos = 0;
	int nCount = pListBox->GetCount();
	int nSelCount = pListBox->GetSelCount();	//http://msdn.microsoft.com/en-us/library/fycfcb17%28v=vs.90%29.aspx
	if ( nSelCount <= 0 )
	{
		return;
	}

	aArray.SetSize(nSelCount);

//DEBUT Simple s�lection
	if ( nSelCount == LB_ERR )	//If the list box is a single-selection list box, the return value is LB_ERR (-1).
	{
		int iPos = pListBox->GetCurSel();
		aArray.SetAt(0,iPos);
		return;
	}
//FIN Simple s�lection

//DEBUT Get the indexes of all the selected items in aryListBoxSel
	pListBox->GetSelItems(nSelCount, aArray.GetData()); 
	AFXDUMP(aArray);	// Dump the selection array.
//FIN  Get the indexes of all the selected items in aryListBoxSel

	return;
}

//-----------------------------------------------------------------------
void Func_1::SetListBox_Selected( CListBox* pListBox, CArray<int,int> &aArray)	//S�lectionne les �lements d�fin dans aArray
{
	int iPos = 0;
	int nSelCount = aArray.GetCount();
	int nCount = pListBox->GetCount();

//DEBUT Re-S�lection des items
	for(int i = 0; i < nCount; i++)
	{
		pListBox->SetSel( i, FALSE);
	}
	for(int i = (nSelCount-1); i >=0; i--)
	{
		iPos = aArray.GetAt(i);
		if ( iPos != (nCount - 1) )
		{
			pListBox->SetSel( iPos + 1, TRUE);
		}
		else
		{
			pListBox->SetSel( 0, TRUE);
		}
	}
//FIN Re-S�lection des items
	return;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions