CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    325

    ListBox not working

    Hello,

    Why cant I insert a string to my ListBox?

    When I click on the button nothing happens.

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    #define IDB_GO  117
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hInstance;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
                       LPSTR Args, int WinMode)
    {
        HWND hWnd;
        MSG Message;
        WNDCLASSEX WinClass;
    
        hInstance = hThisInst;
    
        WinClass.cbSize = sizeof(WNDCLASSEX);
        WinClass.hInstance = hThisInst;
        WinClass.lpszClassName = "Window";
        WinClass.lpfnWndProc = WindowFunc;
        WinClass.style = 0;
        WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
        WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        WinClass.lpszMenuName = NULL;
        WinClass.cbClsExtra = 0;
        WinClass.cbWndExtra = 0;
        WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        if(!RegisterClassEx(&WinClass)) return 0;
    
        hWnd = CreateWindow("Window",
                            "Window",
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            700,
                            600,
                            HWND_DESKTOP,
                            NULL,
                            hThisInst,
                            NULL);
    
        ShowWindow(hWnd,
                   WinMode);
        UpdateWindow(hWnd);
    
        while(GetMessage(&Message,
                         NULL,
                         0,
                         0))
        {
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }
        return Message.wParam;
    }
    
    LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
        HWND hListBox;
        int i;
        char Output[255];
    
        switch(Message)
        {
        case WM_CREATE:
            hListBox = CreateWindow("LISTBOX",
                                    NULL,
                                    WS_CHILD | WS_VISIBLE,
                                    //WS_CHILD | WS_VISIBLE | LBS_MULTICOLUMN, //multi column
                                    10,
                                    10,
                                    500,
                                    500,
                                    hWnd,
                                    (HMENU) 500,
                                    hInstance,
                                    NULL);
    
            SendMessage(hListBox,LB_SETCOLUMNWIDTH,(WPARAM) 100,(LPARAM) 0);
    
            for (i=0; i<22; i++)
            {
                sprintf(Output, "String %d", i);
                SendMessage(hListBox, LB_ADDSTRING, (WPARAM) 0, (LPARAM) Output);
            }
    
            CreateWindow("Button", "GO",
                         WS_VISIBLE |  WS_CHILD,
                         520, 20, 100, 25,
                         hWnd,
                         (HMENU)IDB_GO, 0, NULL);
    
    
            return 0;
    
        case WM_COMMAND:
            switch(wParam)
            {
            case IDB_GO:
                SendMessage(hListBox, LB_INSERTSTRING, (WPARAM) 3, (LPARAM) "aaaaaaaaaaaaaa");
                break;
            }
            return 0;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hWnd, Message, wParam, lParam);
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,724

    Re: ListBox not working

    The problem is this line:

    Code:
    HWND hListBox;
    hListBox is initialised in WM_CREATE, but as it's a local variable it's value is not available when used in WM_COMMAND. The fix is:

    Code:
    static HWND hListBox;
    so that the value of hListBox is maintained between the various calls to WindowFunc()
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.5.3)

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    325

    Re: ListBox not working

    Ah, OK, I understand, so there is a difference between declaring and initializing.

    Since it was declared before WM_CREATE I thought it would be global in WindowFunc.

    Thank you very much and Happy New Year!

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,724

    Re: ListBox not working

    Since it was declared before WM_CREATE I thought it would be global in WindowFunc.
    No. Variables defined in a function have a life-time of the life of the function. When the function starts they are created and when it exits they are destroyed. The WindowsFunc() function is entered separately for each message received. So the values of variables changed by processing one message are 'lost' when the function ends.

    But for a static variable, it is initialised once the first time the function is entered to its default value (0 for int etc). Any changes made to that variable during the function are 'remembered' the next time the function is entered. So if you want variable values to be 'remembered' between each call to the function then make them static.
    https://en.cppreference.com/w/cpp/la...ocal_variables
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.5.3)

  5. #5
    Join Date
    Dec 2007
    Location
    France
    Posts
    325

    Re: ListBox not working

    That was really helpful.

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