Solved

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

Posted on 2014-03-06
7
1,188 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

737 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