Click here to Skip to main content
15,884,388 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.7K   418   8   2
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:

C++
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:

C++
//
#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 ",":
    C++
    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 ","
    C++
    m_vChoosenList // in example below
C++
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

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

 
GeneralMy vote of 3 Pin
M.Ebrahimi.Ahouei14-Oct-13 19:58
M.Ebrahimi.Ahouei14-Oct-13 19:58 
short & good
QuestionMissing download Pin
Smitha Nishant14-Oct-13 13:42
protectorSmitha Nishant14-Oct-13 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.