CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    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'.

      Code:
      //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;
          return;
        }
        
        *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 03:50 PM.

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