# Visual C++ & C++ Programming > C++ and WinAPI >  ListBox not working

## MasterDucky

Hello,

Why cant I insert a string to my ListBox?

When I click on the button nothing happens.



```
#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);
}
```

----------


## 2kaud

The problem is this line:



```
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:



```
static HWND hListBox;
```

so that the value of hListBox is maintained between the various calls to WindowFunc()

----------


## MasterDucky

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!

----------


## 2kaud

> 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

----------


## MasterDucky

That was really helpful.

----------

