//这里啦
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if source=listbox1 then
accept:=true;end;procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
tempstr:string;
P_move:Tpoint;
begin
{通过坐标获得ITEM}
P_move.x:=x;
p_move.y:=y;
if (listbox1.itemindex<>-1) and (listbox1.ItemAtPos(P_move,true)<>-1) then
begin //互换ITEM
tempstr:=listbox1.Items.Strings[listbox1.itemindex];
listbox1.Items.Strings[listbox1.itemindex]:=listbox1.items.Strings[listbox1.ItemAtPos(P_move,true)];
listbox1.items.Strings[listbox1.ItemAtPos(P_move,true)]:=tempstr;
end;
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if source=listbox1 then
accept:=true;end;procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
tempstr:string;
P_move:Tpoint;
begin
{通过坐标获得ITEM}
P_move.x:=x;
p_move.y:=y;
if (listbox1.itemindex<>-1) and (listbox1.ItemAtPos(P_move,true)<>-1) then
begin //互换ITEM
tempstr:=listbox1.Items.Strings[listbox1.itemindex];
listbox1.Items.Strings[listbox1.itemindex]:=listbox1.items.Strings[listbox1.ItemAtPos(P_move,true)];
listbox1.items.Strings[listbox1.ItemAtPos(P_move,true)]:=tempstr;
end;
end;
解决方案 »
- 予人玫瑰,手有余香,希望好心人能帮帮忙!!??谢谢!!!!!!!!!!
- 如何获得打印机的状态?高分求教!急!急!急!
- 我想要一个工具,能识别出这个Delphi 程序里所用到的控件(范围为Delphi自带的控件)也没功夫去写那个只好求助各位。
- 如何遍历所有的菜单和子菜单,得到菜单的caption就可以。
- 如果对A、C、B、5、8、H、5、U进行全排列,数字和字母不能重复,并打印出来,算法如何写[事成后给200分]
- 请问如何使用updatesql控件?
- 我写的Service Application程序为什么总是不能运行?
- 如何做一个进度条显示安装文件的进度?
- 如何使用opc,我从来没有用过,现在要和西门子的plc连接。
- 最近烦死了,一点也不想写程序,各位又过这样的经历吗?怎样调节一下?
- 谁帮我看看怎样才能让服务一直运行而不停止?
- 谁有用DELPHI开发的ASP组件的源代码,请给我一份,万分感激!!!!!!!(81分)
注意两个例子都要设置ListBox1.DragMode:=dmAutomatic;
....
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
SavedIndex:Integer=0;
implementation{$R *.DFM}procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var Arect:TRect;
Aitem:integer;
begin
Accept:=True;
Aitem:=TListBox(Sender).ItemAtPos(point(x,y),TRUE);
Arect:=TListBox(Sender).ItemRect(Aitem);
if Aitem<>SavedIndex then begin
TListBox(Sender).Canvas.Brush.Color:=TListBox(Sender).Color;
TListBox(Sender).Canvas.FrameRect(TListBox(Sender).ItemRect(SavedIndex));
SavedIndex:=Aitem;
end;
TListBox(Sender).Canvas.Brush.Color:=clInactiveBorder;
TListBox(Sender).Canvas.FrameRect(TListBox(Sender).ItemRect(Aitem));
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
ListBox1.Items.Exchange(TListBox(Sender).ItemIndex,TListBox(Sender).ItemAtPos(Point(x,y),True));
end;procedure TForm1.ListBox1StartDrag(Sender: TObject;
var DragObject: TDragObject);begin
SavedIndex:=TListBox(Sender).ItemIndex;
end;
ListBox1.DragMode:=dmAutomatic;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if Source is TListBox then Accept:=True;
而且不要用强制转换,delphi提供了一个类的转换操作符 as
(Source as TListBox).Items...
这样你写了一次之后,以后每次要拖拽的时候都只要将代码复制一下就可以了,什么都不用改。
把
if Aitem<>SavedIndex then begin
改为
if (Aitem<>SaveIndex) and (Aitem>=0) then begin
就ok
begin
ListBox1.Items.Exchange(TListBox(Sender).ItemIndex,TListBox(Sender).ItemAtPos(Point(x,y),True));
end;这里应该也要加入那句
即if ListBox(Sender).ItemAtPos(Point(x,y),True))>=0 then
ListBox1.Items.Exchange(TListBox(Sender).ItemIndex,TListBox(Sender).ItemAtPos(Point(x,y),True));