Nagendram Banukobhan
Nagendram Banukobhan

Reputation: 31

how to get Selected treeview item text

I'm using WINAPI Treeview to show some data. I have to get the selected TV_ITEM text, when I select a TV ITEM. I'm using following code

case WM_NOTIFY:
 switch( ((LPNMHDR)lParam)->code)
 {
 case TVN_SELCHANGED:
  {
         NMTREEVIEW* pnmtv = (LPNMTREEVIEW)lParam;
         LPTSTR str = (pnmtv->itemNew.pszText);
         MessageBox( hWnd, str,"load",MB_OK );
  }
  break;                        
}

I can't get the correct value, I got some kind of garbage value. MessageBox shows some kind of special characters(Those are not in keyboard)

Shall anyone help me, how to get the selected item text. Thanks in advance

Upvotes: 3

Views: 3861

Answers (1)

panda-34
panda-34

Reputation: 4209

Have you read the documentation on TVN_SELCHANGED? It never mentions pszText. You need to

TVITEM item;
item.hItem = pnmtv->itemNew.hItem;
item.mask = TVIF_TEXT;
item.pszText = // allocate buffer
item.cchTextMax = // length of buffer
::SendMessage(hWnd, TVM_GETITEM, 0, (LPARAM)&item);

and then you'll get item.pszText

P.S. If you're working with windows API, how often have you seen Windows allocating and managing strings for you? It just doesn't happen.

Upvotes: 3

Related Questions