Very simple example with Listbox in winapi

15,649

Solution 1

The 1st parameter of SendMessage should be the listbox handle, not the parent window handle.

Solution 2

Make hWndList static by defining

static HWND hWndList;

Then try like this

 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"name");
 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"extension");
 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"date");
 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"size");
Share:
15,649
abilash
Author by

abilash

Updated on June 05, 2022

Comments

  • abilash
    abilash almost 2 years

    How can I display in MessageBox text selected in ListBox? I try to use next code, but it display only blank MessageBox

    wchar_t listBoxStr[15];
    HWND hWndList;
    
    switch (message)
    {
    case WM_CREATE:
        hWndList = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL, 240, 40, 150, 20, hwnd, (HMENU)105, NULL, NULL);
        SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"name");
        SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"extension");
        SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"date");
        SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"size");
        CreateWindowEx(NULL, TEXT("button"), TEXT("FIND"), WS_VISIBLE | WS_CHILD, 410, 40, 50, 20, hwnd, (HMENU)106, NULL, NULL);
        return 0 ;
        return 0 ;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case 106:
            SendMessageW(hWndList, LB_GETTEXT,0,(LPARAM)listBoxStr);
            MessageBoxW(NULL, listBoxStr, L"", NULL);
            return 0;
        }
        return 0;
    case WM_PAINT:
        hdc = BeginPaint (hwnd, &ps);
        GetClientRect (hwnd, &rect) ;
        EndPaint (hwnd, &ps) ;
        return 0 ;
    case WM_DESTROY:
        PostQuitMessage (0) ;
        return 0 ;
    }
    
  • abilash
    abilash over 11 years
    I don't know why but in that case I've got an error "Variable hWndList is being used without is being initialized", but I've initialized it in WM_Create. Doesn't it? Look edition of qestion.
  • Andrey
    Andrey over 11 years
    @Vsevywniy: it should be a member variable
  • abilash
    abilash over 11 years
    In that way it also return first item text value. I need selected one.
  • Andrey
    Andrey over 11 years
    @Vsevywniy: See the answer to stackoverflow.com/questions/739095/…