Solved

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

Posted on 2014-03-06
7
1,038 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
why to use string 10 56
Using Powershell to monitor file changes then execute a command 5 97
zeroMAx challenge 20 77
seriesUp challenge 7 107
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now