Thread: ListView single select not working

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    903

    ListView single select not working

    I added LVS_SINGLESEL to the windows styles but still I can select multiple rows.

    What am I doing wrong?

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <stdio.h>
    #pragma comment(lib, "gdi32.lib")
    #pragma comment(lib, "comctl32.lib")
    
    #include <iostream>
    using namespace std;
    
    #define IDB_ADD     1001
    #define IDB_REMOVE  1002
    #define IDB_GETINFO 1003
    
    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      = { 0 };
        INITCOMMONCONTROLSEX icc = { 0 };
    
        hInstance = hThisInst;
    
        icc.dwSize = sizeof(icc);
        icc.dwICC = ICC_LISTVIEW_CLASSES;
        InitCommonControlsEx(&icc);
    
        WinClass.cbSize        = sizeof(WNDCLASSEX);
        WinClass.hInstance     = hThisInst;
        WinClass.lpszClassName = TEXT("ListViewSample");
        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(TEXT("ListViewSample"),
                            TEXT("List View Sample"),
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            500,
                            400,
                            NULL,
                            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)
    {
        static HWND hListView;
        LVCOLUMN lvc = { 0 };
        LVITEM   lvi = { 0 };
        char Text[1024];
        int i=0;
    
        switch(Message)
        {
        case WM_CREATE:
        {
            hListView = CreateWindow(WC_LISTVIEW,
                                     NULL,
                                     WS_CHILD | WS_VISIBLE | LVS_REPORT| LVS_SINGLESEL,
                                     0,
                                     0,
                                     500,
                                     300,
                                     hWnd,
                                     (HMENU) 500,
                                     hInstance,
                                     NULL);
    
            ListView_SetExtendedListViewStyle(hListView, LVS_EX_CHECKBOXES |
                                              LVS_EX_FULLROWSELECT |
                                              LVS_EX_HEADERDRAGDROP |
                                              LVS_EX_GRIDLINES);
    
            lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;
            lvc.fmt  = LVCFMT_LEFT;
    
            /* Add four columns to the list-view (first column contains check box). */
            lvc.iSubItem = 0;
            lvc.cx       = 50;
            lvc.pszText  = TEXT("Record");
            ListView_InsertColumn(hListView, 0, &lvc);
    
            lvc.iSubItem = 1;
            lvc.cx       = 100;
            lvc.pszText  = TEXT("Name");
            ListView_InsertColumn(hListView, 1, &lvc);
    
            lvc.iSubItem = 2;
            lvc.cx       = 70;
            lvc.pszText  = TEXT("Episodes");
            ListView_InsertColumn(hListView, 2, &lvc);
    
            lvc.iSubItem = 3;
            lvc.cx       = 150;
            lvc.pszText  = TEXT("Any Good?");
            ListView_InsertColumn(hListView, 3, &lvc);
    
            /* Add some rows. iItem=rows */
            lvi.iItem = 0;
            ListView_InsertItem(hListView, &lvi);
            ListView_SetItemText(hListView, 0, 1, TEXT("Friends"));
            ListView_SetItemText(hListView, 0, 2, TEXT("500"));
            ListView_SetItemText(hListView, 0, 3, TEXT("Alright"));
            ListView_SetCheckState(hListView, 0, TRUE);
    
            lvi.iItem = 1;
            ListView_InsertItem(hListView, &lvi);
            ListView_SetItemText(hListView, 1, 1, TEXT("Survivor"));
            ListView_SetItemText(hListView, 1, 2, TEXT("970"));
            ListView_SetItemText(hListView, 1, 3, TEXT("Please, not again"));
            ListView_SetCheckState(hListView, 1, FALSE);
    
            CreateWindow("button", "Add",
                         WS_VISIBLE | WS_CHILD,
                         20, 330, 80, 20,
                         hWnd, (HMENU) IDB_ADD, NULL, NULL);
    
            CreateWindow("button", "Remove",
                         WS_VISIBLE | WS_CHILD,
                         120, 330, 80, 20,
                         hWnd, (HMENU) IDB_REMOVE, NULL, NULL);
    
            CreateWindow("button", "Info",
                         WS_VISIBLE | WS_CHILD,
                         240, 330, 80, 20,
                         hWnd, (HMENU) IDB_GETINFO, NULL, NULL);
    
            break;
        }
    
      
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        default:
            return DefWindowProc(hWnd,Message,wParam,lParam);
        }
        return 0;
    }
    Windows 10 with Visual Studio 14.0.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    124
    What Windows version do you use?
    On my Win 7 64bit I can only select a single row

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    903
    Windows 10 and Visual Studio 14.0.

    Thanks for the response, I tried now myself with a Win 7 64 bit and I can select both. Strange.
    Last edited by Ducky; 01-05-2018 at 03:01 PM.
    Windows 10 with Visual Studio 14.0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to select on the valid listview items?
    By mark103 in forum C++ Programming
    Replies: 20
    Last Post: 05-06-2012, 04:57 PM
  2. Replies: 4
    Last Post: 06-22-2008, 11:55 PM
  3. ListView is not working!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2007, 02:25 PM
  4. Replies: 6
    Last Post: 07-10-2006, 12:05 AM
  5. Select entire row of listview (using win32)
    By Mokkan in forum Windows Programming
    Replies: 2
    Last Post: 06-02-2005, 12:51 PM

Tags for this Thread