急急急!谁能帮助我(200分)

  • 主题发起人 主题发起人 WHY&SONG
  • 开始时间 开始时间
W

WHY&SONG

Unregistered / Unconfirmed
GUEST, unregistred user!
我用一个treeview 控件显示一个表中的部门,用listview控件显示该部门的人员,我想使用拖放操作,实现人员在部门之间的调动,请问如何实现?
 
看我写的例子,注意listview的dragmode改成dmAutomatic
(我没有连接数据库,你可以编程或使用控件来使用数据库,我演示的是drag&drop)

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;

type
TForm1 = class(TForm)
TreeView1: TTreeView;
ListView1: TListView;
procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ListView1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var curTreeNode:TTreeNode;

procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
LISTVIEW1.BeginDrag(TRUE);
end;

procedure TForm1.ListView1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
LISTVIEW1.EndDrag(TRUE);
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if source is TLISTVIEW then accept:=true else Accept:=false;
end;

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
curTreeNode:=treeview1.droptarget;
WITH LISTVIEW1 DO
BEGIN
treeview1.Items.AddChild(curTreeNode,listview1.selected.caption);
END;
end;

end.

 
谢谢你,但你能不能再帮我一下,一.我拖放的时候,TREEVIEW控件可以显示得到焦
点的fctreenode,我用mouseover事件,但坐标计算总是不对,可能是方法错了.
二.同时拖放多条纪录,我该如何处理.
 
> curTreeNode:=treeview1.droptarget;
> treeview1.Items.AddChild(curTreeNode,listview1.selected.caption);
这两句话就是表明把listview里面的item拖放到获得焦点的treeview的节点
用的是dragover 事件,不是mouseover

同时拖放多条纪录:
这个时候listview1.selected指的是第一条被选中的item。可以用getnextitem来
获得下一条。把dragdrop事件代码改成:

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var i:integer;
nextitem:Tlistitem;
begin
curTreeNode:=treeview1.droptarget;
i:=1;
WITH LISTVIEW1 DO
BEGIN
treeview1.Items.AddChild(curTreeNode,listview1.selected.caption);
nextitem:=selected;
while i<selcount do
begin
//获得下一个item
nextitem:=GetNextItem(nextitem,sdall,[isSelected]);
treeview1.Items.AddChild(curTreeNode,nextitem.caption);
i:=i+1;
end;
END;
end;

 
sorry,再来一编

procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var i:integer;
nextitem:Tlistitem;
begin
curTreeNode:=treeview1.droptarget;
i:=1;
WITH LISTVIEW1 DO
BEGIN
treeview1.Items.AddChild(curTreeNode,listview1.selected.caption);
nextitem:=selected;
while i<selcount do
begin
nextitem:=GetNextItem(nextitem,sdall,[isSelected]);
treeview1.Items.AddChild(curTreeNode,nextitem.caption);
i:=i+1;
end;
END;
end;
 
咦?为什么老是把我的程序吃掉一点?
while 循环的条件是i小于selcount //好像我不能用小于号
begin
nextitem:=GetNextItem(nextitem,sdall,[isSelected]);
treeview1.Items.AddChild(curTreeNode,nextitem.caption);
i:=i+1;
end;
 
后退
顶部