How can I detect which listbox item another item was dropped onto?
I have two listboxes. The listbox1 contains a list of the DB names. Listbox2, on the other hand, has a list the titles of the content associated by the DB on listbox1. Basically you click on listbox1 and it loads into listbox2 all the titles for the content of the DB.
I want to implement now a drag and drop feature. I know how to drag between two listboxes; that's not the problem. What I am trying to implement is the following:
click on a title in listbox2
drag item INTO an item in lisbox1
the title is now part of the DB pointed by the item in listbox1
Now, all the backend code to move the actual data is already in coded. How can I make the listbox1 select (and know) the item where the mous开发者_Python百科e is about to drop the item from the listbox2? By implementing a simple drag and drop between the two listboxes will result on the item from listbox2 being added into listbox1 since I cannot select an item in listbox1 while i am dragging something.
I hope I explained this the right way.
Code is appreciated.
If I understand correctly, you're trying to see what item is being dropped on. What you need is the ItemAtPos
function of the ListBox. It takes a TPoint
parameter and the OnDragDrop
event handler has the X and Y coordinates.
In this example, ListBox2 is the Source, and ListBox1 is the control being dropped onto. iItem
gives me the ItemIndex
of the ListBox1 item being dropped onto.
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
iItem: Integer;
MyPoint: TPoint;
begin
MyPoint.X := X;
MyPoint.Y := Y;
iItem := ListBox1.ItemAtPos(MyPoint, True);
ListBox1.Items.Insert(iItem, ListBox2.Items[ListBox2.ItemIndex]);
end;
There is no range checking here, it's just an example to illustrate the ItemAtPos
function.
精彩评论