📄 dragf.pas
字号:
unit DragF;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, StdCtrls, CheckLst, ExtCtrls;
type
TDragForm = class(TForm)
ListBox1: TListBox;
CheckListBox1: TCheckListBox;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure ListDragOver(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);
procedure Edit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function AddNotDup (List: TCustomListBox; Text: string): Boolean;
end;
var
DragForm: TDragForm;
implementation
{$R *.DFM}
procedure TDragForm.ListDragOver(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 TDragForm.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
I: Integer;
begin
if Source = Edit1 then
// 复制Edit控件中的文本
ListBox1.Items.Add (Edit1.Text)
else if Source = CheckListBox1 then
begin
// 复制所有选中的项目
// 删除选中的项目
for I := CheckListBox1.Items.Count - 1 downto 0 do
if CheckListBox1.Checked [I] then
begin
if AddNotDup (ListBox1, CheckListBox1.Items [I]) then
CheckListBox1.Items.Delete (I);
end;
end;
end;
procedure TDragForm.CheckListBox1DragDrop(Sender, Source: TObject; X,
Y: Integer);
var
nItem: Integer;
begin
if Source = Edit1 then
// 复制Edit控件中的文本
CheckListBox1.Items.Add (Edit1.Text)
else if Source = ListBox1 then
begin
// 复制ListBox中被选中的项
nItem := ListBox1.ItemIndex;
if AddNotDup (CheckListBox1, ListBox1.Items [nItem]) then
// 删除被选中的项目
ListBox1.Items.Delete (nItem);
end;
end;
procedure TDragForm.Edit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Edit1.BeginDrag (False, 10);
end;
function TDragForm.AddNotDup (List: TCustomListBox; Text: string): Boolean;
begin
// 判断项目是否存在
Result := List.Items.IndexOf (Text) < 0;
if Result then
List.Items.Add (Text);
end;
procedure TDragForm.Button1Click(Sender: TObject);
//添加项目
begin
if not(edit1.Text='') then
ListBox1.items.Add(edit1.text);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -