February 14th, 2003, 09:21 AM
MFC Tree Control: How to disable an item?
Q: How to disable an item?
A: Tree items do not have enabled/disabled states. That means that you can stop searching for a function called 'SetItemEnabled()', because it does not exist. You have to simulate this by your own. Let's outline what you need to do in order to 'disable' an item:
- You need to mark that item as 'disabled'. As the tree control itself doesn't help here, you need to reserve a flag in the underlaying structure of each item. You will use 'SetItemData()' and 'GetItemData()' to set and retrieve this structure for each item.
- You need to give some visual feedback to your user that the item is disabled. The easiest solution is to use a special (grayed) icon for the item. You can refine this approach up to changing the items color or font, as presented in this CodeGuru article.
- You need to prevent some operations on that item, like expanding, selecting, dragging it or dropping on it. We assume that for each item you have correctly set an underlaying data structure called 'CItemStruct', and this has a boolean member 'm_bDisabled'.
//Preventing selection: (handle TVN_SELCHANGING)
void CYourDialog::OnSelchangingTree(NMHDR* pNMHDR, LRESULT* pResult)
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*) pNMHDR;
if(((CItemStruct *) m_tree.GetItemData(pNMTreeView->iNewItem))->m_bDisabled)
*pResult = 1;
*pResult = 0;
Handle the same way:
- 'TVN_ITEMEXPANDING' to prevent expanding. (be sure to collapse items prior to disabling them!)
- 'TVN_BEGINDRAG' to prevent dragging that item
- 'TVN_BEGINLABELEDIT' to prevent editing it
- and so on.
Last edited by Andreas Masur; July 25th, 2005 at 04:50 PM.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!