Thread: List View troubles (adding items)

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    List View troubles (adding items)

    I did some searching on here, msdn, and codeguru and didn't find anything I was looking for. I cannot successfully add an item to my list view. I have 3 columns in my list view, all of which are visible, but when I try to add an item so I can add text the ListView_InsertItem() macro fails every time. I don't know where I went wrong. I basically took novacain's code from searches and still nothing. Here is my function, and I call it at the end of WM_INITDIALOG.

    Code:
    bool AddItem(int index, int col, char* text, HWND hwnd)
    {
        LVITEM LvI;
        LvI.mask = LVIF_TEXT | LVIF_PARAM;
        LvI.iItem = index;
        LvI.iImage = 0;
        LvI.state = 0;
        LvI.iSubItem = 0;
        LvI.pszText = text;
        LvI.cchTextMax = lstrlen(text);
        LvI.lParam = 0;
        if(ListView_InsertItem(hwnd, &LvI))
            MessageBox(hwnd,"item added","",MB_OK); //this message shows if i put
                                                   //if(!ListView_InsertItem(blah blah)) so it's returning -1
                
        if(col > 0)
            ListView_SetItemText(hwnd, 1,1,text);
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    There are a few issues with your AddItem function.. But anyway, here's a basic fully functional example that you can compare to your code.

    Code:
    #pragma comment(lib, "comctl32.lib")
    #pragma comment(lib, "user32.lib")
    
    #include <windows.h>
    #include <commctrl.h>
    
    #define CLASS_NAME "My Example Class Name"
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    HWND hWnd,hWndLV;          
    HWND hWndAddButton, hWndEditAdd1, hWndEditAdd2, hWndEditAdd3;      
    
    int WinMain(  HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpCmdLine,
                         int nCmdShow)
    {
        INITCOMMONCONTROLSEX iccx;
        WNDCLASSEX wcx = {0};
        MSG msg;
    
        iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
        iccx.dwICC = ICC_LISTVIEW_CLASSES;
        InitCommonControlsEx(&iccx);
    
        wcx.cbSize = sizeof(wcx);
        wcx.hbrBackground = (HBRUSH)COLOR_WINDOW;
        wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcx.hInstance = hInstance;
        wcx.lpfnWndProc = WndProc;
        wcx.lpszClassName = CLASS_NAME;
    
        RegisterClassEx(&wcx);
    
        hWnd = CreateWindowEx(0, CLASS_NAME, "Example Window",
            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 450, 350,
            0, 0, hInstance, 0);
    
        if(hWnd == NULL)
            return 0;
        hWndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, "",
            LVS_SHOWSELALWAYS | WS_BORDER | WS_CHILD | WS_VISIBLE | LVS_REPORT,
            6, 64, 306, 200, hWnd, NULL, hInstance, NULL);
        hWndAddButton = CreateWindowEx(0, "BUTTON", "Add",
            WS_CHILD | WS_VISIBLE | WS_TABSTOP,
            6, 6, 75, 23, hWnd, NULL, hInstance, NULL);
        hWndEditAdd1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Sample Data",
            WS_BORDER | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
            87, 6, 75, 23, hWnd, NULL, hInstance, NULL);
        hWndEditAdd2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "100",
            WS_BORDER | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
            168, 6, 75, 23, hWnd, NULL, hInstance, NULL);
        hWndEditAdd3 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "30",
            WS_BORDER | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
            248, 6, 75, 23, hWnd, NULL, hInstance, NULL);       
        LV_COLUMN   lvc; 
        lvc.mask = LVCF_TEXT | LVCF_WIDTH;
        lvc.pszText = "Text";
        lvc.cx = 100;
        ListView_InsertColumn(hWndLV, 0, &lvc);
        lvc.pszText = "Numbers1";
        ListView_InsertColumn(hWndLV, 1, &lvc);
        lvc.pszText = "Numbers2";
        ListView_InsertColumn(hWndLV, 2, &lvc);
        ShowWindow(hWnd,  SW_SHOW);
        while(GetMessage(&msg, 0, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return 0;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
            case WM_COMMAND:
                {
                    if((HWND)lParam == hWndAddButton)
                    {
                        CHAR szText1[200] = {0};
                        CHAR szText2[200] = {0};
                        CHAR szText3[200] = {0};
                        INT iIndex = 0;
                        LVITEM lvi;
                        GetWindowText(hWndEditAdd1, szText1, 200);
                        GetWindowText(hWndEditAdd2, szText2, 200);
                        GetWindowText(hWndEditAdd3, szText3, 200);
    
                        ZeroMemory(&lvi, sizeof(lvi));
                        lvi.mask = LVIF_TEXT | LVIF_PARAM;
                        lvi.pszText = szText1;
                        lvi.iItem = ListView_GetItemCount(hWndLV);
                        iIndex = ListView_InsertItem(hWndLV, &lvi);
                        ListView_SetItemText(hWndLV, iIndex, 1, szText2);
                        ListView_SetItemText(hWndLV, iIndex, 2, szText3);
                        return 0;
                    }
                    break;
                }
            case WM_CLOSE:
                {
                    DestroyWindow(hWnd);
                    return 0;
                }
            case WM_DESTROY:
                {
                    PostQuitMessage(0);
                    return 0;
                }
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by scwizzo View Post
    I did some searching on here, msdn
    ...and didn't find anything
    Ae you joking ?
    http://msdn.microsoft.com/en-us/libr...36(VS.85).aspx
    and I don't talk about all samples in KB, PSDK and all Listview articles from MSDN...

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Alex31 View Post
    Ae you joking ?
    http://msdn.microsoft.com/en-us/libr...36(VS.85).aspx
    and I don't talk about all samples in KB, PSDK and all Listview articles from MSDN...
    If you quote what I said you should have added the "I was looking for" part. I know how to add text, I just didnt find anything saying why my add text wasn't working. Bob, I will take a look at your code and see if I can work something out.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Ok... I figured it out. There was some issues with my AddItem function, and I am still working that out. The main thing that was wrong was I had LVS_OWNERDRAWFIXED as one of the window styles >.< Now I can see text.

    Once I figure out why the InsertItem() isn't working right I will post what I did for that.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    So all is well... kinda. I can add text, and across each column for a certain row, however, I can't get the text now. My compiler keeps telling me ListView_GetSelectionMark() and other macro's/definitions are undefined. I tried using different LVS_* styles, and the compiler does not know those either. I linked to libcommctl32.a and in the manifest DLL version 6.0 is specified. Does anyone have any ideas to what is happening here? I am running Vista if that makes any difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  2. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  3. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  4. List View Control troubles
    By cppdude in forum Windows Programming
    Replies: 2
    Last Post: 05-07-2002, 02:56 PM