CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 1999
    Location
    Malmoe, Sweden
    Posts
    112

    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

  2. #2
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    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

  3. #3
    Join Date
    Nov 1999
    Location
    Malmoe, Sweden
    Posts
    112
    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

  4. #4
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    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

  5. #5
    Join Date
    Mar 2002
    Location
    Kyiv, Ukraine
    Posts
    87
    Maybe pParent is not valid, try to assing pParent = this.

  6. #6
    Join Date
    May 2002
    Posts
    1
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured