Solved

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

Posted on 2014-03-06
7
1,217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 34

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

707 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