# [win32] changing control style

## Recommended Posts

Hi, Is there a way to change a control's style after it has been created? Or do you have to recreate the entire control? eg: changing LBS_SORT style. (sorting/unsorting a listbox) Thanks.

##### Share on other sites
SetWindowLong (or its slightly more up to date amd64 friendly companion, SetWindowLongPtr()) can do this, among other things.

##### Share on other sites
Thanks for the advice, that function does infact seem to allow you to re-set a controls style, but it does not seem to have any effect on a listbox's sorting property.

i=GetWindowLong(GetDlgItem(hWnd,IDC_DIRLIST2),GWL_style);if (i&LBS_SORT) {	MessageBox(NULL,"sorted","",MB_OK);	};SetWindowLong(GetDlgItem(hWnd,IDC_DIRLIST2),GWL_style,i|LBS_SORT);ORSetWindowLong(GetDlgItem(hWnd,IDC_DIRLIST2),GWL_style,i&(~LBS_SORT));//re-populate listbox..

The code appears to correctly set/unset the sorted state, but this doesn't seem to effect the listbox itself.

Do I need to call some other function to force this to work?

Thanks.

##### Share on other sites
Have you tried starting out with an unsorted Listbox and a non-alphabetical set of strings... and then setting the style to LBS_SORT?

What happens when adding a string to the Listbox after unsetting LBS_SORT? Does it continue to sort?

##### Share on other sites
Yes, I have tried that already, but no luck. Thanks for the suggestion though.

Apparently the people over at code project have run into the same problem:
Quote:
 When the style is modified on a listbox to include or remove the LBS_SORT for example, it can be seen (by using Getstyle(), or using a tool such as Spy++) that the control does in fact have the new style, and yet the control's behaviour has not changed.

It appears the only solution is to recreate the listbox control.

But if anyone knows of a simpler way, I'd like to hear it. :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978753

• 21
• 14
• 12
• 39