Keeping the indentation level with Drag '& Drop | CodeGuru

Keeping the indentation level with Drag ‘& Drop

I can suggest an extension to the Drag and Drop of your TreeView control. The standard drag and drop as in your control will add the drag branch as a child of the drop item. Quite often during drag and drop operations parent items should stay parent items, child items should stay child items, grandchildren […]

Written By
CodeGuru Staff
CodeGuru Staff
May 15, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I can suggest an extension to the Drag and Drop of your TreeView
control. The standard drag and drop as in your control will add the drag
branch as a child of the drop item. Quite often during drag and drop
operations parent items should stay parent items, child items should stay
child items, grandchildren should stay grandchildren, etc, etc.

The following allows just that – with a few logical rules of course:

  1. Dragging an item onto another item on the same indentation level (no
    matter in which branch), will add the dragitem as a sibling of the dropitem,
    directly following the dropitem.
  2. Dragging an item onto an item of the next higher (parent-level)
    indentation, will add the dragitem as the last child of the dropitem.

Therefore the indentation level of the dragitem will stay the same after
dropping it. Dragging onto lower (child-level) indentation level, and
dragging onto more than 1 higher (parent) indentations are not allowed.

Add a member variable:

   BOOL m_bKeepIndentLevel;

In the constructor, initialize this variable:

m_bKeepIndentLevel = FALSE;

When this member is FALSE, the drag and drop operation will work as it
currently is. To keep the indentation level, you can change this value to
TRUE inside your program, either by directly accessing the variable, or by
Get/Set functions.

In the OnLButtonUp() function, add/modify the lines indicated by ‘->’ :

void CTreeCtrlX::OnLButtonUp(UINT nFlags, CPoint point)
{
      .
      .
      .
      while ((htiParent = GetParentItem(htiParent)) != NULL)
      {
         if (htiParent == m_hDragItem) return;
      }
->    // check for same indentation level
->    HTREEITEM htiPosition = TVI_LAST;
->    if (m_bKeepIndentLevel)
->    {
->       int nDragIndent = GetIndentLevel(m_hDragItem);
->       int nDropIndent = GetIndentLevel(m_hDropItem);
->
->       if (nDragIndent == nDropIndent)
->       {
->          // add after the drop item, as child of drop item’s parent
->          htiPosition = m_hDropItem;
->          m_hDropItem = GetParentItem(m_hDropItem);
->       }
->       else if (nDragIndent == (nDropIndent + 1))
->       {
->          // if dropped on ‘upper’ indentation, add as last child of drop item
->          htiPosition = TVI_LAST;
->       }
->       else
->       {
->          // invalid drop target
->          return;
->       }
->    }
      Expand (m_hDropItem,TVE_EXPAND);
->    HTREEITEM htiNew = CopyBranch(m_hDragItem,m_hDropItem,htiPosition);
      DeleteItem(m_hDragItem);
      .
      .
      .
}

The implementation of GetIndentLevel() is as follows:

int CRTreeCtrl::GetIndentLevel(HTREEITEM hItem)
{
   int iIndent = 0;
   while ((hItem = GetParentItem(hItem)) != NULL)
   {
      iIndent++;
   }
   return iIndent;
}

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.