listbox多选状态下的自拖与互拖[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 yanleigis 的 blog

unit unit1;

file://yanlei:e_mail yanleiigis@21cn.com

interface

 

uses
    windows, messages, sysutils, variants, classes, graphics, controls, forms,
    dialogs, stdctrls;

type
    tform1 = class(tform)
        listbox1: tlistbox;
        listbox2: tlistbox;
        procedure listbox1dragdrop(sender, source: tobject; x, y: integer);
        procedure listbox1dragover(sender, source: tobject; x, y: integer;
            state: tdragstate; var accept: boolean);
        procedure formcreate(sender: tobject);
    private
    { private declarations }
    public
    { public declarations }
    end;

var
    form1: tform1;

implementation

{$r *.dfm}

procedure tform1.listbox1dragover(sender, source: tobject; x, y: integer;
    state: tdragstate; var accept: boolean);
var
    apoint: tpoint;
begin
    apoint.x := x;
    apoint.y := y;


    if (source is tlistbox) then
        if ((source as tlistbox).selcount > 0) and
            ((sender as tlistbox).itematpos(apoint, true) <> -1)
            and ((sender as tlistbox).itematpos(apoint, true) <> (sender as tlistbox).itemindex) then
        begin
            accept := true;
        end;
end;

 

procedure tform1.listbox1dragdrop(sender, source: tobject; x, y: integer);
var
    apoint: tpoint;
    st: string;
    p: integer;
    i, num: integer;
    mylbox: tstrings;
begin
    apoint.x := x;
    apoint.y := y;

    p := (sender as tlistbox).itematpos(apoint, true);

    mylbox := tstringlist.create;
    try
        num := (source as tlistbox).items.count;
        for i := num - 1 downto 0 do
        begin
            if (source as tlistbox).selected[i] then
            begin
                st := (source as tlistbox).items[i];
                mylbox.add(st)

            end;
        end;
        if p > -1 then
        begin
            for i := 0 to mylbox.count - 1 do
            begin

                (sender as tlistbox).items.insert(p, mylbox[i])

            end;
        end
        else
        begin
            for i := mylbox.count - 1 downto 0 do
            begin
                (sender as tlistbox).items.add(mylbox[i])

            end;

        end;
        (source as tlistbox).deleteselected;
        if p > -1 then
            (sender as tlistbox).selected[p] := true;
    finally
        mylbox.free;

本文关键:自拖与互拖
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top