Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Drag/Drop from on ListBox to another?

Posted on 1999-07-29
5
Medium Priority
?
378 Views
Last Modified: 2013-11-20
I'm using MFC, and trying to figure out how (or if its possible) to be able to drag an item from one CListBox and drop it in another CListBox.

I created a sample app to figure this out that has just 2 ListBox's using the resource editor. But I just can't seem to figure it out whatsoever! Any help, or sample code/project, etc? I'm not all that experienced in this type of thing.....

Thanks ;)
0
Comment
Question by:gunn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 1311976
Look at this sample project... its for tree control and list control. but you will get the idea on how to accomplish it for list box..


SAMPLE: MFCDRAG Implements Drag&Drop Between CListCtrl & CTree

http://support.microsoft.com/support/kb/articles/q148/7/38.asp
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 1311977
Also look at the use of CDragListBox usage... although it is for drag and drop with in same ListBox.
0
 

Author Comment

by:gunn
ID: 1311978
According to: http://www.codeguru.com/mfc/comments/7446.shtml

CListBox is not a type of CView, so it will never work this way! The example at the MS site shows both being a type of CView.....
0
 
LVL 5

Accepted Solution

by:
vachooho earned 200 total points
ID: 1311979
You must overwrite the following functions in your ListBox

DROPEFFECT CGridCtrlEx::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
     return DROPEFFECT_COPY;// or DROPEFFECT_MOVE
     // add your own processing
}

DROPEFFECT CGridCtrlEx::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
{
    return DROPEFFECT_COPY;
}

void CGridCtrlEx::OnDragLeave()
{
  // clear some internal variables you created during
  // drag/droping
}

BOOL CGridCtrlEx::OnDrop(COleDataObject* pDataObject, DROPEFFECT /*dropEffect*/, CPoint /* point */)
{
    // do actual coping       

    return TRUE; // if you done coping
    // or false if not
}


Also you must register your window as drop target if you need drop effect
Create some derived from COleDropTarget class, which will redirect Drag/drop messages to your window

class CGridDropTarget : public COleDropTarget
{
public:
    CGridDropTarget()
      {
          m_pGridCtrl            = NULL;
          m_bRegistered      = FALSE;
      };
    virtual ~CGridDropTarget() {};

// Attributes
public:
    CGridCtrlEx *      m_pGridCtrl;
    BOOL                  m_bRegistered;

// Operations
public:
    BOOL Register(CGridCtrlEx * pGridCtrl)
      {
            if(m_bRegistered)
                  return FALSE;

            m_pGridCtrl = pGridCtrl;

            m_bRegistered = COleDropTarget::Register(pGridCtrl);

            return m_bRegistered;
      };
    virtual void Revoke()
      {
            m_bRegistered = FALSE;
            COleDropTarget::Revoke();
      };

    BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
      {
            ASSERT(m_pGridCtrl != NULL);

            if(pWnd->GetSafeHwnd() == m_pGridCtrl->GetSafeHwnd())
                  return m_pGridCtrl->OnDrop(pDataObject, dropEffect, point);
            else
                  return FALSE;
      };
    DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
      {
            ASSERT(m_pGridCtrl != NULL);

            if(pWnd->GetSafeHwnd() == m_pGridCtrl->GetSafeHwnd())
                  return m_pGridCtrl->OnDragEnter(pDataObject, dwKeyState, point);
            else
                  return DROPEFFECT_NONE;
      };
    void OnDragLeave(CWnd* pWnd)
      {
            ASSERT(m_pGridCtrl != NULL);

            if (pWnd->GetSafeHwnd() == m_pGridCtrl->GetSafeHwnd())
                  m_pGridCtrl->OnDragLeave();
      };
    DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)
      {
            ASSERT(m_pGridCtrl != NULL);

            if (pWnd->GetSafeHwnd() == m_pGridCtrl->GetSafeHwnd())
                  return m_pGridCtrl->OnDragOver(pDataObject, dwKeyState, point);
            else
                  return DROPEFFECT_NONE;
      };
    DROPEFFECT OnDragScroll(CWnd* pWnd, DWORD dwKeyState, CPoint point)
      {
            ASSERT(m_pGridCtrl != NULL);

            if(pWnd->GetSafeHwnd() == m_pGridCtrl->GetSafeHwnd())
            {
                  if (dwKeyState & MK_CONTROL)
                        return DROPEFFECT_COPY;
                  else
                        return DROPEFFECT_MOVE;
            }
            else
                  return DROPEFFECT_NONE;
      };
};

instantiate this class within your ListBox and call
m_pDropTarget = new CGridDropTarget;
m_pDropTarget->Register(this);
where this is the pointer to  your ListBox

to start drag/drop operation
do the follwing in you WM_MOUSEMOVE handler
{
   if(m_nMouseInternalMode == MOUSE_PREPARE_TO_GRAG)
   {
       HGLOBAL hMem = NULL;
       // TODO  Place your data into hMem
       COleDataSource * pData = new COleDataSource();
       pData->CacheGlobalData(CF_TEXT, hMem);

      if(pData != NULL)
      {
      DROPEFFECT de = pData->DoDragDrop(DROPEFFECT_COPY);
      delete pData;
      }
   }
}

That's all
Hope this will help

0
 

Expert Comment

by:dave_p
ID: 1954808
Have you got the draging from list box to list box figureed out? I'm just
now looking at the same thing. Any suggestions? It looks pretty messy to
me...

Dave P
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question