Solved

CListbox "Sort" property: how to change it during runtime

Posted on 2014-03-06
7
1,112 Views
Last Modified: 2014-03-14
I have a CListbox control on a CDialog in my VC++ MFC application.  In the resource editor (ie, in design time), I have the "Sort" property intentionally set to FALSE.    However, I'd like to be able to change the value of the CListbox "Sort" property to TRUE while the program is running. For example, if the user clicks a button on the dialog, change the "Sort" property to TRUE.

I know I could "manaully" sort the contents of the CListbox at runtime (load them into a CString array, alphabetize them, clear the CListbox contents, and then reload the now-alphabetized contents in the CString array back into the CListbox).  Tha'ts fine.  I'd also just like to know how to programmatically set the "Sort" property.

Thanks very much!
0
Comment
Question by:cjm20
  • 5
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39910981
That's actually pretty straightfoward, you just need to set 'LBS_SORT' via 'SetWindowLong()' (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx):

HWND hwndListBox =   GetDlgItem( hDlg, ID_MY_LISTBOX ); 
DWORD dwStyle = GetWindowLong(hwndListBox , GWL_STYLE);
dwStyle = dwStyle | LBS_SORT; // enable 'sort'
SetWindowLong( hwndListBox , GWL_STYLE, dwStyle ); // apply it

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39911093
BTW, you can also disable that the same way:

HWND hwndListBox =   GetDlgItem( hDlg, ID_MY_LISTBOX ); 
DWORD dwStyle = GetWindowLong(hwndListBox , GWL_STYLE);
dwStyle = dwStyle & ~LBS_SORT; // disable 'sort'
SetWindowLong( hwndListBox , GWL_STYLE, dwStyle ); // apply it
                                            

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39911251
As a side note - you can do that also by deriving from CListBox and adding the above code as methods. Then you can obtain the window handle via 'GetSafeHwnd()' (inherited from CWnd).
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 86

Expert Comment

by:jkr
ID: 39911462
To extend my lat comment, that could be like


class CMySortEnableableListBox : public CListBox
{
// other stuff...
public:

  void EnableSort()
  {
    HWND hwnd =   GetSafeHwnd();
    DWORD dwStyle = GetWindowLong(hwnd , GWL_STYLE);
    dwStyle = dwStyle | LBS_SORT; // enable 'sort'
    SetWindowLong( hwnd , GWL_STYLE, dwStyle ); // apply it                                          
  }

  void DisableSort()
  {
    HWND hwnd =   GetSafeHwnd();
    DWORD dwStyle = GetWindowLong(hwnd , GWL_STYLE);
    dwStyle = dwStyle & L+BS_SORT; // disable 'sort'
    SetWindowLong( hwnd , GWL_STYLE, dwStyle ); // apply it                                          
  }

// more stuff
};

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39914223
Do you have any further questions regarding detaild on that issue?
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39916457
the LBS_SORT flag cannot be added after the listbox was created. even with code like

void CTestdlgDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here

    DWORD dwStyle = GetWindowLong(m_listbox.m_hWnd , GWL_STYLE);
    dwStyle = dwStyle | LBS_SORT; // enable 'sort'
    SetWindowLong( m_listbox.m_hWnd , GWL_STYLE, dwStyle ); // apply 
    m_listbox.ResetContent();
    m_listbox.AddString("XYZ");
    m_listbox.AddString("ABC");
    m_listbox.AddString("DEF");
    m_listbox.AddString("CCC");

}

Open in new window


you would not be able to get the strings displayed sorted if the sort flag was not set initially in the resources or at create time.

a way out is to have a pointer to CListBox in your dialog and create the listbox on the fly (dynamically) and if the sort needs to be switched, you would destroy the control and create a new one with the sort flag given. an easier way (probably) is to have the listbox twice, one is hidden and sorted and one is visible and not sorted. both listbox controls are at the same place and you disable-hide or show-enable the one or the other.

my way would be to use a list control instead of a listbox. here the list may be unsorted initially but would be sorted if the user clicks into the list header.

Sara
0
 

Author Closing Comment

by:cjm20
ID: 39930198
thanks Sara, and thanks JKR.  I tried setting the sort property as you'd described, using SetWindowLong(....).  No compile errors of course, but it also did not affect the sorting in the list box.  So the long way to go (two listboxes or dynamic creation / deletion of the list box) is going to be the way to go for me.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question