Delphi列表框拖放多个项目
我需要将多个项目拖放到 Tlistbox
中。
我所指的代码是
var
StartingPoint : TPoint;
implementation
...
procedure TForm1.FormCreate(Sender: TObject) ;
begin
ListBox1.DragMode := dmAutomatic;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer) ;
var
DropPosition, StartPosition: Integer;
DropPoint: TPoint;
begin
DropPoint.X := X;
DropPoint.Y := Y;
with Source as TListBox do
begin
StartPosition := ItemAtPos(StartingPoint,True) ;
DropPosition := ItemAtPos(DropPoint,True) ;
Items.Move(StartPosition, DropPosition) ;
end;
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean) ;
begin
Accept := Source = ListBox1;
end;
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer) ;
begin
StartingPoint.X := X;
StartingPoint.Y := Y;
end;
来自这里。
它工作正常,但我需要实现的是这样的 。
为什么我想要这个是因为这些列表框项目有一定的对应顺序。 因此,我不想手动选择每个项目并拖放它,而是想启用多个拖放。
任何有关我如何实现这一目标的意见都将受到赞赏。
如果可以使用其他组件,也可以建议使用其他组件。
I need to Drag and Drop multiple items inside my Tlistbox
.
The code which I am referring to is
var
StartingPoint : TPoint;
implementation
...
procedure TForm1.FormCreate(Sender: TObject) ;
begin
ListBox1.DragMode := dmAutomatic;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer) ;
var
DropPosition, StartPosition: Integer;
DropPoint: TPoint;
begin
DropPoint.X := X;
DropPoint.Y := Y;
with Source as TListBox do
begin
StartPosition := ItemAtPos(StartingPoint,True) ;
DropPosition := ItemAtPos(DropPoint,True) ;
Items.Move(StartPosition, DropPosition) ;
end;
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean) ;
begin
Accept := Source = ListBox1;
end;
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer) ;
begin
StartingPoint.X := X;
StartingPoint.Y := Y;
end;
from here.
It works fine but what I need to achieve is like this.
Why I want this is because there is certain order that corresponds to these listbox items.
So instead of just having manually select each item and drag drop it,I want to enable multiple drag drop.
Any views on how can I achieve this is appreciated.
Also can suggest the use of other components if the following is possible using the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要做好这一点是非常棘手的(请参阅我对此答案的第一次修订,了解如何出错的示例)。
这是一种相当容易理解的方法,它通过以下方式解决问题:
现在,可以通过一系列
Move
调用来完成此操作,但很难做到正确。每次移动时,所选项目的所有索引都会发生变化。我上面给出的方法是我解决这个问题的首选方法。顺便说一句,我最近在树视图的上下文中解决了完全相同的问题,而且那里也很棘手!This is surprisingly tricky to do well (see my first revision of this answer for an example of how to get it wrong).
Here's a rather easy to understand approach that solves the problem by:
Now, it is possible to do this with a series of calls to
Move
but it's hard to get it right. Each time you make a move all the indices of the selected items change. The approach I give above is my preferred method for solving this problem. Incidentally I recently worked on exactly the same problem in the context of a tree view and it's quite tricky there too!