Board index » cppbuilder » TListBox drag&drop problem

TListBox drag&drop problem

I have a demo from 'C++ Builder How to..' using two list boxes from which
items can be dragged from one box and dropped in the second. However, after
creating a new project in BCB5 and coping the code there is still a problem.
The compiler complains about

      listbox->BeginDrag (false) ;
 as BeginDrag now requires two parameters, so I changed it to the following

  TListBox *listbox = (TListBox *) Sender ;
  // Make sure we are actually on an item on the list.
  if (listbox->ItemAtPos (Point (X, Y), true) >= 0)
    listbox->BeginDrag (false, 0) ;

and also
     listbox->BeginDrag (false, 10etc) ;

The programme compiles, but it is not possible to drag the components as the
example does.

Can anyone help?

Cheers.

 

Re:TListBox drag&drop problem


Hi Bob,

Quote
> I have a demo from 'C++ Builder How to..' using two list boxes
> from which items can be dragged from one box and dropped in the
> second.
[snip]
> The programme compiles, but it is not possible to drag the
> components as the example does.

Ensure that the DragMode property of the source ListBox is set to
dmManual.  Also, are you properly handling the OnDragOver event
handler by manipulating the Accept parameter?  If you still can't get
things working, compare your code with the following example...

void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
   TMouseButton Button, TShiftState Shift, int X, int Y)
{
   if (DragDetect(ListBox1->Handle, Point(X, Y)))
   {
      const int drag_index =
         ListBox1->ItemAtPos(Point(X, Y), true);
      ListBox1->Tag = drag_index;      
      if (drag_index != -1)
      {
         ListBox1->BeginDrag(false, 0);
      }
   }

Quote
}

void __fastcall TForm1::ListBox2DragOver(TObject *Sender,
   TObject *Source, int X, int Y, TDragState State, bool &Accept)
{
   Accept = (Source == ListBox1);  

Quote
}

void __fastcall TForm1::ListBox2DragDrop(TObject *Sender,
   TObject *Source, int X, int Y)
{
   if (Source == ListBox1 && ListBox1->Tag != -1)
   {
      const int drop_index =
         ListBox2->ItemAtPos(Point(X, Y), true);
      ListBox2->Items->Insert(
         drop_index,
         ListBox1->Items->Strings[ListBox1->Tag]
         );
   }

Quote
}

Best of luck!

--
Damon Chandler (TeamB - C++Builder)
- http://homepages.go.com/~damonchandler/vcl_faq.zip
- http://bcbcaq.freeservers.com

Re:TListBox drag&drop problem


Will this allow multiple items to be dragged?

Bob

"Damon Chandler (TeamB)" <dm...@cornell.edu> wrote in message
news:39E02482.85162C5B@cornell.edu...

Quote
> Hi Bob,
> > I have a demo from 'C++ Builder How to..' using two list boxes
> > from which items can be dragged from one box and dropped in the
> > second.
> [snip]
> > The programme compiles, but it is not possible to drag the
> > components as the example does.

> Ensure that the DragMode property of the source ListBox is set to
> dmManual.  Also, are you properly handling the OnDragOver event
> handler by manipulating the Accept parameter?  If you still can't get
> things working, compare your code with the following example...

> void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
>    TMouseButton Button, TShiftState Shift, int X, int Y)
> {
>    if (DragDetect(ListBox1->Handle, Point(X, Y)))
>    {
>       const int drag_index =
>          ListBox1->ItemAtPos(Point(X, Y), true);
>       ListBox1->Tag = drag_index;
>       if (drag_index != -1)
>       {
>          ListBox1->BeginDrag(false, 0);
>       }
>    }
> }

> void __fastcall TForm1::ListBox2DragOver(TObject *Sender,
>    TObject *Source, int X, int Y, TDragState State, bool &Accept)
> {
>    Accept = (Source == ListBox1);
> }

> void __fastcall TForm1::ListBox2DragDrop(TObject *Sender,
>    TObject *Source, int X, int Y)
> {
>    if (Source == ListBox1 && ListBox1->Tag != -1)
>    {
>       const int drop_index =
>          ListBox2->ItemAtPos(Point(X, Y), true);
>       ListBox2->Items->Insert(
>          drop_index,
>          ListBox1->Items->Strings[ListBox1->Tag]
>          );
>    }
> }

> Best of luck!

> --
> Damon Chandler (TeamB - C++Builder)
> - http://homepages.go.com/~damonchandler/vcl_faq.zip
> - http://bcbcaq.freeservers.com

Other Threads