How can I drag items between two ListBoxes?
Applies To: C++Builder 1 or higher
Category: Knowledge Base
- Start a new project using File | New Application.
-
- Add two (2) ListBoxes. Set the OnMouseDown, OnDragOver
- and OnDragDrop events of both of them to:
-
- void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
- TMouseButton Button, TShiftState Shift, int X, int Y)
- {
- if(Button == mbLeft)
- {
- TListBox *list = (TListBox *)Sender;
- if(list->ItemAtPos(Point(X, Y), true) >= 0)
- list->BeginDrag(false, -1);
- }
- }
-
- void __fastcall TForm1::ListBox1DragOver(TObject *Sender, TObject
- *Source, int X, int Y, TDragState State, bool &Accept)
- {
- if(Source->InheritsFrom(__classid(TListBox)))
- Accept = true;
- }
-
- void __fastcall TForm1::ListBox1DragDrop(TObject *Sender, TObject
- *Source, int X, int Y)
- {
- if(Source->InheritsFrom(__classid(TListBox)))
- {
- TListBox *listsender = (TListBox *)Sender;
- TListBox *listsource = (TListBox *)Source;
-
- listsender->Items->Add(listsource->Items->Strings
- [listsource->ItemIndex]);
- listsource->Items->Delete(listsource->ItemIndex);
- }
- }
C++Builder Developer's Network
Copyright © Yoto Yotov