X
xiaolin317
Unregistered / Unconfirmed
GUEST, unregistred user!
写了一个小东西,把listbox中的内容拖动到checklistbox中,或者把checklistbox中的内容拖动到listbox中。可DragDrop事件怎么样也触发不了是怎么回事呀?
下面是源码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, CheckLst;
type
TForm1 = class(TForm)
ListBox1: TListBox;
CheckListBox1: TCheckListBox;
Edit1: TEdit;
Button1: TButton;
procedure ListBox1DragOver(Sender, Source: TObject;
X, Y: Integer;
State: TDragState;
var Accept: Boolean);
procedure ListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
procedure CheckListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
function addnotdup(list:Tcustomlistbox;text:string):boolean;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.addnotdup(list:Tcustomlistbox;text:string):boolean;
begin
result:=list.items.indexof(text)<0;
if result then
list.items.add(text);
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject;
X, Y: Integer;
State: TDragState;
var Accept: Boolean);
begin
accept:=true;
if (source=edit1) and ((sender as tcustomListBox).items.IndexOf (edit1.text)>=0) then
accept:=false;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
var
i:integer;
begin
if source=edit1 then
listbox1.Items.Add(edit1.text)
else
if source=CheckListBox1 then
begin
for i:=CheckListBox1.Items.Count-1do
wnto 0do
if CheckListBox1.Checked then
begin
if addnotdup(listbox1,checklistbox1.Items) then
checklistbox1.Items.Delete(i);
end;
end;
end;
procedure TForm1.CheckListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
var
nItem:integer;
begin
if source=edit1 then
CheckListBox1.Items.Add(edit1.Text)
else
if source=listbox1 then
begin
nItem:=listbox1.ItemIndex;
if addnotdup(checklistbox1,listbox1.Items[nItem]) then
listbox1.Items.Delete(nitem);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CheckListBox1.Items.Add(edit1.Text);
ListBox1.Items.Add(edit1.Text);
end;
end.
下面是源码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, CheckLst;
type
TForm1 = class(TForm)
ListBox1: TListBox;
CheckListBox1: TCheckListBox;
Edit1: TEdit;
Button1: TButton;
procedure ListBox1DragOver(Sender, Source: TObject;
X, Y: Integer;
State: TDragState;
var Accept: Boolean);
procedure ListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
procedure CheckListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
function addnotdup(list:Tcustomlistbox;text:string):boolean;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.addnotdup(list:Tcustomlistbox;text:string):boolean;
begin
result:=list.items.indexof(text)<0;
if result then
list.items.add(text);
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject;
X, Y: Integer;
State: TDragState;
var Accept: Boolean);
begin
accept:=true;
if (source=edit1) and ((sender as tcustomListBox).items.IndexOf (edit1.text)>=0) then
accept:=false;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
var
i:integer;
begin
if source=edit1 then
listbox1.Items.Add(edit1.text)
else
if source=CheckListBox1 then
begin
for i:=CheckListBox1.Items.Count-1do
wnto 0do
if CheckListBox1.Checked then
begin
if addnotdup(listbox1,checklistbox1.Items) then
checklistbox1.Items.Delete(i);
end;
end;
end;
procedure TForm1.CheckListBox1DragDrop(Sender, Source: TObject;
X,
Y: Integer);
var
nItem:integer;
begin
if source=edit1 then
CheckListBox1.Items.Add(edit1.Text)
else
if source=listbox1 then
begin
nItem:=listbox1.ItemIndex;
if addnotdup(checklistbox1,listbox1.Items[nItem]) then
listbox1.Items.Delete(nitem);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CheckListBox1.Items.Add(edit1.Text);
ListBox1.Items.Add(edit1.Text);
end;
end.