如何在ListBox中用鼠标拖动改变Item在ListBox中的先后顺序?(100分)

  • 主题发起人 主题发起人 luaijun
  • 开始时间 开始时间
L

luaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
最好给段源码,非常感谢
 
procedure lbMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure lbMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure lbMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);

var
isdown:boolean;
source:integer;
target:integer;



procedure Tripmain.lbMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
source:=lb.ItemAtPos(point(x,y),false);
isdown:=true;
end;

procedure Tripmain.lbMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
isdown:=false;
end;

procedure Tripmain.lbMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if isdown then
begin
target:=lb.ItemAtPos(point(x,y),false);
if (source<>target) and (target>=0) then
begin
lb.Items.Exchange(target,source);
source:=target;
end;
end;
end;
 
以前看到一段代码,他的是通过按钮来实现上下移动,不过类似啦,可以给你参考一下
procedure Tfrmqryconfig.btnBottClick(Sender: TObject);
begin
MoveSelectedItems('Bott');
end;

procedure Tfrmqryconfig.btnNextClick(Sender: TObject);
begin
MoveSelectedItems('Next');
end;

procedure Tfrmqryconfig.btnPreClick(Sender: TObject);
begin
MoveSelectedItems('Pre');
end;

procedure Tfrmqryconfig.btnTopClick(Sender: TObject);
begin
MoveSelectedItems('Top');
end;

procedure Tfrmqryconfig.MoveSelectedItems(act: string);
var
i,iMove:integer;
begin
if not (activecontrol is TListBox) then
begin
application.MessageBox('请选择列表框','提示',MB_OK);
exit;
end;

with TListBox(activecontrol) do
begin
if SelCount < 1 then exit;

if (act = 'Next') or (act = 'Bott') then
begin
if Selected[Items.Count-1] then exit;
iMove := 0;
for i := Items.Count - 1 downto 0 do
begin
if Selected then
begin
if act = 'Next' then
begin
if i < Items.Count - 1 then
begin
Items.Move(i,i+1);
Selected[i+1] := true;
end;
end
else
begin
if iMove = 0 then
iMove := Items.Count - 1 - i;
Items.Move(i,i+iMove);
Selected[i+iMove] := true;
end
end;
end; //for
exit;
end; //if (act = 'Next') or (act = 'Bott') then

if Selected[0] then exit;
iMove := 0;
for i:=0 to Items.Count - 1 do
begin
if Selected then
begin
if act = 'Pre' then
begin
if i > 0 then
begin
Items.Move(i,i-1);
Selected[i-1] := true;
end;
end
else if act = 'Top' then
begin
if iMove = 0 then
iMove := i;
Items.Move(i,i-iMove);
Selected[i-iMove] := true;
end;
end; //if Selected then
end; //for
end; //with

end;
 
呵呵,DELPHI好像有DEMO的吧。自己找找。里面很详细
 
利用 DragDrop 和 DragOver 事件:
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
Idx: Integer;
Pos: TPoint;
begin
with Sender as TListBox do
begin
Pos.X := X;
Pos.y := Y;
Idx := ItemAtPos(Pos, True);
Items.Move(ItemIndex, Idx);
ItemIndex := Idx;
end;
end;

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
Idx: Integer;
Pos: TPoint;
begin
Accept := False;
if Sender is TListBox then
with Sender as TListBox do
begin
Pos.X := X;
Pos.y := Y;
Idx := ItemAtPos(Pos, True);
if (Idx > -1) and (idx <> ItemIndex) then
Accept := True;
end;
end;

注意 ListBox 的 DragKind 属性应设为 dkDrag,DragMode 应为 dmAutomatic。
 
对不起!第二个事件处理过程的函数定义是错的,Copy 后忘了改 :( ,更正如下:
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
Idx: Integer;
Pos: TPoint;
begin
Accept := False;
if Sender is TListBox then
with Sender as TListBox do
begin
Pos.X := X;
Pos.y := Y;
Idx := ItemAtPos(Pos, True);
if (Idx > -1) and (idx <> ItemIndex) then
Accept := True;
end;
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=402151
 
多人接受答案了。
 
后退
顶部