Solved

Using SetWindowLong (MSVC)

Posted on 1998-10-06
20
461 Views
Last Modified: 2008-03-03
Why does the listbox freeze when i use LBS_SORT in SetWindowLong()?

      case IDC_BUTTON15:
            if(HIWORD(wp)==BN_CLICKED)
            {
      /*            LONG SetWindowLong(
    HWND hWnd,      // handle of window
    int nIndex,      // offset of value to set
    LONG dwNewLong       // new value*/


SetWindowLong((HWND)GetDlgItem(hw,IDC_LIST1),(int)GWL_STYLE,(LONG)LBS_SORT);
UpdateWindow(GetDlgItem(hw,IDC_LIST1));
            }
      break;

Your sincerely
Andla.
0
Comment
Question by:andla
[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
  • 9
  • 5
  • 3
  • +1
20 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1174558
One problem (maybe not the only one) is that you are setting only the LBS_SORT stile and clearing all the other styles.  That is probably not what you want.  There are styles there, like WS_CHILD at least, that you want to have remain.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174559
What you should do is use GetWindowLong() to get the previou style and then set the LBS_SORT stile by ORing it into the old stule with the operator |.  Then set the window style to this new value.

Let me know if that doesn't clear it up.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174560
For setting the style you can also try ModifyStyle MFC function which is handy

What nietod means is

DWORD dwStyle = GetWindowLong( HWND)GetDlgItem(hw,IDC_LIST1),GWL_STYLE) ;
dwStyle |= LBS_SORT ;
SetWindowLong((HWND)GetDlgItem(hw,IDC_LIST1),(int)GWL_STYLE,(LONG)dwStyle);


0
Independent Software Vendors: 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 1

Author Comment

by:andla
ID: 1174561
Thanks for your answer :-), but my problem has changed from freezing to not sorting the items?? I was hoping that the items should sort up after i pressed the button.
Please help me!

Your sincerely
Andla
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174562
I beleive that changing the style will cause it to add new items sorted, not to sort what is there.  Remove the items and add them again and they will be sorted.
0
 
LVL 1

Author Comment

by:andla
ID: 1174563
Not my day to day. It doesn't work anything. Maybe to much coffee :-). Here is some code.
if(HIWORD(wp)==BN_CLICKED)
{
//SendDlgItemMessage(hw,IDC_LIST1,LB_RESETCONTENT,0,0);
DWORD dwStyle = GetWindowLong((HWND)GetDlgItem(hw,IDC_LIST1),GWL_STYLE) ;

dwStyle |= LBS_SORT ;

SetWindowLong((HWND)GetDlgItem(hw,IDC_LIST1),(int)GWL_STYLE,(LONG)dwStyle);
SendDlgItemMessage(hw,IDC_LIST1,LB_RESETCONTENT,0,0);
//UpdateWindow(GetDlgItem(hw,IDC_LIST1));
SendDlgItemMessage(hw,IDC_LIST1,LB_ADDSTRING,0,(LPARAM)"c");
SendDlgItemMessage(hw,IDC_LIST1,LB_ADDSTRING,0,(LPARAM)"b");
SendDlgItemMessage(hw,IDC_LIST1,LB_ADDSTRING,0,(LPARAM)"a");
}
break;
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174564
You are adding the strings to the wrong window.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1174565
LBS_SORT is not a style that can be changed at runtime.  You have to destroy the window and create a new one with the LBS_SORT flag set.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174566
Opps, no you're not.  I though you were using SendMessage(), not SendDlgItemMessage(), and thus sending the add message to the dialog.  Sorry.

I don't see a problem.   I wonder why do you have so many casts? You should change the style to a LONG.  Then the only casts you need are the (LPARAM) when you add the strings.  Try to clean that up maybe somthing will turn up.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174567
>> LBS_SORT is not a style that can be changed at runtime
Are you sure of that?  Most styles (maybe all) can be, and this would certainly be one that that would be desirable.

It that is the case, I guess you would have to resort to sorting it yourself, but I'm not convinced unless danny_pav has some documentation for that.
0
 
LVL 1

Author Comment

by:andla
ID: 1174568
Maybe something else is wrong. I have noticed that the HSCROLL doesent work from this resource:
    LISTBOX         IDC_LIST1,1,153,96,57,LBS_NOINTEGRALHEIGHT | WS_VSCROLL |
                    WS_HSCROLL | WS_TABSTOP,WS_EX_STATICEDGE
If i understand it should letting me scroll horizontal if the string is longer than the listbox.? When i build my application, i'm not splitting the program over many files. Instead i'm using just one huge c file. I don't know if bugs appears if my .c file is to big? That could be the case why things don't work. (but i hope not)

Your sincerely
Andla.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174569
>> I don't know if bugs appears if my .c file is to big
not likely.   If there are bugs, they are probably your doing.

Out of curiosity, what happens when you specify the LBS_SORT style initially (i.e. in the resources).  Does it sort then?

Also did you check that

GetDlgItem(hw,IDC_LIST1)

is returning the right handle?  (also, you may want to save that in a local variables so you don't have to keep calling GetDlgItem()).
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1174570
LBS_SORT is not a style that can be changed at runtime.  You have to destroy the window and create a new one with the LBS_SORT flag set.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174571
Well here's a trick I use when wanting to change the control:

1. Create a dialog with 2 listboxs (say IDC_LIST1, IDC_LIST2) at exactly same position with different style

2. Keep contents in sync (always add same contents to both lists)

3. When you want to switch modes, hide one and show the other, e.g.

// hide list 1
EnableWindow( GetDlgItem(hDlg,IDC_LIST1), FALSE ) ;
ShowWindow( GetDlgItem(hDlg,IDC_LIST1), SW_HIDE ) ;

// show list 2
EnableWindow( GetDlgItem(hDlg,IDC_LIST2), TRUE ) ;
ShowWindow( GetDlgItem(hDlg,IDC_LIST2), SW_SHOW ) ;

4. Keep track of which list is active in a boolean or integer variable.

5. In WM_INITDIALOG use tip #3 and tip #4 to set the initial control shown

Using this "work round" you can give the appearance (to the user) of changing styles radically on the fly.  This approach isn't limited to 2 list boxes (or even two identical controls) by the way
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174572
It might be easier to create one that is always sorted and use LB_ADDSTRING to add sorted items and LB_INSERTSTRNG to add them unsorted (or move them about).

But I still find it hard to believe that the sort style can't be changed.
0
 
LVL 1

Author Comment

by:andla
ID: 1174573
I suppose you are right. Thanks! But the problem is still there and this sample code worked fine so it must be the right window. To complete this question i need to know what i shall do to make an alfabetic order of all the items. One way should be to call a function that sorted the strings and then i could call LB_RESETCONTENT and LB_ADD in a loop to build up the items. The second solution should be to destroy the window and use CreateWindowEx() to build a new one but how do i keep the ID_ ?. The third solution should be to use two listboxes over eatch other so it look like one control, but the problem is that i cannot (i think) have two controls with the same ID_ so my functions works on both listboxes.

I need to know how to write the code.

Your sincerly
andla.
0
 
LVL 3

Accepted Solution

by:
danny_pav earned 50 total points
ID: 1174574
1.  Make the list box sorted in the resources.
2.  Add items when unsorted by using LB_INSERTSTRING to the end
3.  Add items when sorted using LB_ADDSTRING
4.  When shifting from unsorted to sorted, use an array, store all the values, resetcontent and add using add string. also, use SetRedraw to eliminate blinking and speed up the process.

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174575
>> But I still find it hard to believe that the sort style can't be changed.

I saw a comment from MikeB (MFC/MS guy) a while ago that not all controls handle the WM_STYLECHANGED message.  MikeB thought this was reasonable behaviour.  In this case I would say you could argue it might be, although it would certainly be a lot more reasonable in my view if it said somewhere which controls handled it, and which didn't...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174576
>>MikeB thought this was reasonable behaviour
MikeB believes that EVERYTHING that Microsoft does is 100% perfect.  (Don't put MS down on a question he is on, you'll never hear the end of it.  Defenitly don't use the word "bug"!)

In any case, I think the idea of using a sorted list box all the time and using the LB_ADDSTRING  and LB_INSERTSTRING was mine.
0
 
LVL 1

Author Comment

by:andla
ID: 1174577
Thanks! With a little work it now works good with LB_INSERTSTRING. I suppose changing styles on conrols created from the resource is not ment to be done that way. Perhaps it only works with CreateDialog,CreateWindow/Ex().

:-)
Your sincerely
Andla
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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