CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    listview get item text length?

    hi. i need to get the item text length in order to prepare the buffer, how do i do this thanks.

    http://msdn.microsoft.com/en-us/libr...55(VS.85).aspx

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: listview get item text length?

    There is not any function/message which directly returns the text length of a listview item text.
    You have two choices
    1. Allocate a buffer large enough to keep any item text of your listview or, much better...
    2. ...start with a reasonable buffer size and loop until it can keep the text as in the following example
      Code:
         int nSize = 64;
         int nRet = 0;
      
         LVITEM lvi = {0};
         lvi.iSubItem = nSubItem;
         lvi.pszText = NULL;
         do
         {
            nSize *= 2;
            free(lvi.pszText);
            lvi.pszText = (LPTSTR)calloc(nSize, sizeof(TCHAR));
            lvi.cchTextMax = nSize;
            nRet = (int)::SendMessage(hWndList, LVM_GETITEMTEXT, (WPARAM)nItem, (LPARAM)&lvi);
         }while(nRet >= nSize - 1);
    Last edited by ovidiucucu; May 31st, 2009 at 03:43 AM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: listview get item text length?

    thanks alot. i was thinking about #2 but i was hoping there was a better solution...
    LVS_OWNERDATA, seems to be another solution. but that seems like ALOT of work, and in this case its not reallythe best cause its to much work for nothing at all.

    thanks alot for your reply

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: listview get item text length?

    Quote Originally Posted by wigga View Post
    LVS_OWNERDATA, seems to be another solution. but that seems like ALOT of work
    .
    Making a virtual listview control (having LVS_OWNERDATA style) is a completely different approach and is good when we are dealing with very large number of items.
    The list doesn't internally keep any information (text and so on) and ask for them anytime an item has to be displayed (by sending LVN_GETDISPINFO notification to its parent).
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: listview get item text length?

    well i geuss that is really nice to have. not that i have alot of items. (on my windows i have around 270 items) they are just results from the QueryServicesStatus. and display around 6 sub items currently.

    i was thinking keeping the service manager open all the time, and using the callback functions to keep all the info up to date. i made it so when i right click on an item i see the service options menu, to set the status and delete the service(i could seriously mess up my computer by deleting ntfs driver or something :O).

    i am having the folowing issues tho. 1. the full row select style doesnt work it only selects the first item (report view ofcourse), the headers dont automaticly sort the items, i tried using the autosort style but than the windows creation failed.

    here are my current styles:
    Code:
    		inline CREATESTRUCTW m_CreateStruct(LPCWSTR name, int id , HWND parent)
    		{
    			CREATESTRUCTW cs = {0};
    			cs.lpszClass = WC_LISTVIEWW;
    			cs.lpszName = name;
    			cs.hInstance = CApp::Instance();
    			cs.hwndParent = parent;
    			cs.lpCreateParams = this; // doesnt really do anything.
    			cs.hMenu = reinterpret_cast<HMENU>(id);
    			cs.style = LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | WS_CHILD | WS_VISIBLE;
    			cs.dwExStyle = WS_EX_CLIENTEDGE | LVS_EX_FULLROWSELECT | LVS_EX_SINGLEROW;
    			return cs;
    		}
    i do know inline is irrelavant here cause its not going to inline it most likely.
    i create all controls classes, form classes, dialog classes, containers, allocators, string classes from scratch. i know there is STL, BOOST, MFC, wxWidgets, or whatever library, but i like to do evrything from scratch. I think my containers are awesome tho, lots of diffrent containers specialised for any job are good programming design imho.

    okay enough babbling. Do you think in my case it will good to use the "Virtual Listview control?"
    Oh and can you give me a decent tutorial or example =] thanks alot for your time!~

    And i should teach myself to use capitals in evry new sentence.
    Last edited by wigga; May 31st, 2009 at 01:23 PM.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: listview get item text length?

    Quote Originally Posted by wigga View Post
    Do you think in my case it will good to use the "Virtual Listview control?"
    No. A virtual listview control makes sense only for very large number of items (thousands, ten thousands, more...).

    It doesn't internally keep any item data. It has the advantage of avoiding long time of populating many items, but can have a series of disavantages.

    You have already noticed that (auto)sort doesn't work.
    If no item info inside then... what to sort?...
    Last edited by ovidiucucu; May 31st, 2009 at 02:24 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: listview get item text length?

    i thought it would automaticly sort on text.

    can you give me an example how to make sort work? and why doesnt the fll row select work?

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: listview get item text length?

    You can find information about dealing with listview controls in Codeguru Articles
    Also you can see a virtual listview which I made in this little open project: LogExpert (note that you have to be logged-in to download sources).

    Unfortunately, all these are using MFC (not raw-WinAPI), but you can pick some ideas from there.
    Last edited by ovidiucucu; May 31st, 2009 at 04:18 PM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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