65.9K
CodeProject is changing. Read more.
Home

Dialog to select and sort data with CListBox

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (2 votes)

Oct 14, 2013

CPOL

1 min read

viewsIcon

21171

downloadIcon

420

Dialog box to choose data / Items and sort them

Introduction

I needed a dialog box to choose columns from database and sort them. Such dialog is often seen, so I expected to find a ready to use solution on the web. Unfortunately, I found nothing, so I decided to create my own solution.

Background

The part to sort items on the second CListbox is described in article "Moving-listbox-items-up-and-down".

Configuration in Your Project

a) Add the following files in your project:

  • MovListBoxItem.h & .cpp
  • ListBoxMoveItem.h & .cpp
  • WndResizer_2.h & .cpp
  • Function_1D.h & .cpp

b) In Properties / Link / input: Add on 'Additional dependancies': UxTheme.lib (on debug and release)

c) Create a dialog box in your project with the following data:

IDD_DIALOG1 DIALOGEX 0, 0, 301, 277
STYLE DS_SETFONT | DS_FIXEDSYS | DS_CENTER | 
      WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Move ListBox Items and sort"
FONT 8, "MS Shell Dlg", 400, 0, 0x0
BEGIN
    LISTBOX         IDC_LIST1,7,23,115,223,LBS_SORT | LBS_NOINTEGRALHEIGHT | 
                    LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
    LISTBOX         IDC_LIST2,159,23,115,223,LBS_NOINTEGRALHEIGHT | 
                    LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
    PUSHBUTTON      ">",IDC_BUTTON1,128,99,25,14
    PUSHBUTTON      "<",IDC_BUTTON2,128,115,25,14
    PUSHBUTTON      ">>",IDC_BUTTON3,128,139,25,14
    PUSHBUTTON      "<<",IDC_BUTTON4,128,155,25,14
    LTEXT           "Available",IDC_STATIC_AVAILABLE,7,13,33,8
    LTEXT           "Selected",IDC_STATIC_SELECTED,158,13,38,8
    LTEXT           "(x/n)",IDC_STATIC_NBDISPO,49,13,33,8
    DEFPUSHBUTTON   "OK",IDOK,158,253,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,224,253,50,14
    LTEXT           "(x/n)",IDC_STATIC_NB_SEL,201,13,33,8
    PUSHBUTTON      "^",IDC_BUTTON20,278,112,18,19
    PUSHBUTTON      "v",IDC_BUTTON21,278,136,18,19
END

d) Add variables:

  • Control CListBox IDC_LIST1 : m_cListBox_Dispo
  • Control CListBox IDC_LIST2 : m_cListBox_Selection
  • Value Static IDC_STATIC_NBDISPO : m_vNbDispo
  • Value Static IDC_STATIC_NB_SEL : m_vNbSel

e) Include in your code:

//
#include "MovListBoxItem.h"
#include "ListBoxMoveItem.h"   
//

Using the Code

You need to transfer two variables in the dialog:

  • One for the complete list of items: A CString where each item is separated with ",":
    m_vCompleteList // in example below 
  • One for the already selected (and sorted) items previously saved (as settings data for example): A CString where each item is separated with ","
    m_vChoosenList // in example below
    
UpdateData( TRUE );    //FALSE : pour la mise à jour du contrôle 
                       //TRUE : pour récupérer la valeur dans la variable.
CListBoxMoveItem dialog(this, m_vCompleteList, m_vChoosenList);
if (dialog.DoModal() == IDOK)
{
  m_vChoosenList = dialog.Get_ListColSelection();
}
delete dialog;
UpdateData( FALSE );    //FALSE : pour la mise à jour du contrôle 
                        //TRUE : pour récupérer la valeur dans la variable. 

Points of Interest

The demo includes a modified version of WndResizer from Mizan Rahman MFC/C++ Helper Class for Window Resizing (the original can be used, the modification is for hiding/showing controls) .

Remark: I tested multiple selection parts, but not the single selection part.

History

  • 14th October, 2013: First published