-
CListBox notification
Hi!
I have trouble making a CListBox notify its parent window when the selection is about to change (or, more, specifically, when an item is automatically selected by pressing a character key). The legacy code that creates the listbox looks like this:
CreateEx( WS_EX_TOOLWINDOW, _T("LISTBOX"), NULL,
WS_BORDER|WS_VSCROLL|WS_HSCROLL|WS_POPUP,
0,0,0,0, pParent, 0 );
I need to assign the listbox an ID to make it possible to use ON_LBN_SELCHANGE( ID_OF_MY_LISTBOX, MyHandler ) in the parent window to catch the event that the selection is about to change. But I can't. The CreateEx function fails if the last parameter isn't 0. Somewhere in the MFC code I get the error message "Invalid menu ID".
What am I to do to be able to catch the event I'm interested in?
Very greatful for any hints on how to do this.
Magnus
-
1. You use wrong version of CreateEx. Use
BOOL CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam = NULL);
2. ON_LBN_SELCHANGE is macro wrapper for certain kind of WM_COMMAND. So you could process it at the overriden OnCommand, for example
-
I already tried the other function version you mention but it still fails if the nID parameter isn't 0. Actually, your version calls the version I used.
So, CreateEx still fails.
Thanks anyway for your time.
Magnus
-
Yes, You are right. Actually I understood what is the problem. You should try to specify WS_CHILD instead of WS_POPUP. Otherwise your window isn't considered as child
-
Maybe pParent is not valid, try to assing pParent = this.
-
Hi again!
Thanks for your support this far.
It seems like my CListBox must have the WS_POPUP style to work correctly in the current environment (it is the list window of a "combobox" placed in a property list) . I guess there is no way then to notify a parent window if the WS_CHILD style isn't applied? I also see in the documentation that LBN_SELCHANGE is only sent when the list box has the LBS_NOTIFY style and that can't be applied using CWnd::CreateEx, can it?
By the way, how does it work with LBN_SELCHANGE. That is sent "when the selection is about to change" according to documentation. I guess calling GetCurSel would return the selection before change, or am I wrong here? It sounds crazy. How would you get the new selection?
Is there any other way to know when the user pressed a character key (or any key) and then get the new selection without using parent notification? I know I could catch the pressing of a button but then the selection hasn't changed yet.
More or less desperate and very greatful for any hints.
Magnus