Click to See Complete Forum and Search --> : Getting listView item text length
dro873
August 6th, 2005, 12:28 AM
I have a listview control where the items and subitems are all just text. One of the operations of my program is to go through each item and subitem and get the text (e.g., by using LVM_GETITEM or LVM_GETITEMTEXT and passing the address of an LVITEM object).
Both of these methods require that I make a buffer to receive the string. However, I do not know in advance how long the text in each item and subitem will be. My question is: is there some way to get the length of the text of an item or subitem without having already made a buffer?
Thanx in advance.
Runt888
August 6th, 2005, 12:46 AM
Set cchTextMax in the LVITEM structure to 0 and pszText to NULL, then send the LVM_GETITEMTEXT message. It will return the number of characters.
Kelly
dro873
August 6th, 2005, 01:36 AM
LVM_GETITEMTEXT only returns the number of characters the were put in the buffer. I tried it your way but it still just returns zero.
Runt888
August 6th, 2005, 01:51 AM
Oops...You're right, I read the docs wrong. I've been looking around, and it looks like you need to just allocate an array that you think is large enough. Kind of retarded. If you're filling in the list view, you could always store the length of the string in the lParam, assuming you aren't using it for anything else. Kind of a hack, but it will work.
Kelly
humptydumpty
August 6th, 2005, 01:52 AM
if it is in ATL do like this
m_List2.GetItemText(nItem,0,bstrFileName); //bstr Type
and in case of your listView
at the place where length is required
you can use
char str[255];
memset(str,0,sizeof(str)+1); //allocate space to your str
in case of your
int GetItemText(
int nItem//row,
int nSubItem//column,
LPTSTR lpszText//your str,
int nLen //here sizeof(str)+1)
that's all
Hacker2
August 7th, 2005, 06:30 PM
Here is the way MFC does it.
It tries to get the string, and if it's a big string, it allocates more space
CString CListCtrl::GetItemText(int nItem, int nSubItem) const
{
LV_ITEM item;
memset(&item, 0, sizeof(LV_ITEM));
lvi.iSubItem = nSubItem;
CString string;
int Length = 128; //initial reasonable string length
int ReturnCode;
do{
nLen *= 2; //resize the string buffer
item.cchTextMax = Length;
item.pszText = string.GetBufferSetLength(Length);
ReturnCode = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT,
(WPARAM)nItem, (LPARAM)&lvi);
} while (ReturnCode == Length-1); //if could not get all chars, try again
string.ReleaseBuffer();
return string;
}
so if you use a CString for each entry, and save them all in a CStringArray,
you don't have to worry about how many chars in each listctrl entry
humptydumpty
August 8th, 2005, 01:54 AM
so if you use a CString for each entry, and save them all in a CStringArray,
you don't have to worry about how many chars in each listctrl entry
yes that's a good way to store all of your selected item. but don't forget to release you CSimpleArray after completion of your work.
you can do either before filling the array you can check if this has value free them then fill the array.or you can do after performing calculation on your array upto u
dro873
August 8th, 2005, 05:04 PM
Thank you for your solutions. Ultimately I think I'll just rethink my program to eliminate the problem, but you all have thoroughly answered my question. Rating points to all.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.