Delphi 编程实现拖动排序并输出到文档

介绍:实现拖动排序功能,并将排序后的内容输出到文档中。我们将使用 Delphi 的组件来创建一个界面,其中包括一个 Memo 控件用于输入内容,一个 ListBox 控件用于显示排序后的内容,并且提供按钮来触发排序和输出操作。

Delphi 编程实现拖动排序并输出到文档_第1张图片

代码实现:

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   ListBox3.DragMode := dmAutomatic;
end;
procedure TForm1.Button59Click(Sender: TObject);
var
  I: Integer;
  TxtFile: TextFile;
begin
  AssignFile(TxtFile, 'output.txt');
  Rewrite(TxtFile);
  try
    for I := 0 to ListBox3.Items.Count - 1 do
    begin
      WriteLn(TxtFile, ListBox3.Items[I]);
    end;
  finally
    CloseFile(TxtFile);
  end;
  ShowMessage('排序后的内容已输出到output.txt文件');
  ShellExecute(0, 'open', 'notepad.exe', PChar('output.txt'), nil, SW_SHOWNORMAL);
end;
procedure TForm1.Button60Click(Sender: TObject);
var
  I: Integer;
begin
  ListBox3.Clear;
  for I := 0 to Memo4.Lines.Count - 1 do
  begin
    ListBox3.Items.Add(Memo4.Lines[I]);
  end;

end;
procedure TForm1.ListBox3DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i:integer;
str:string;
begin
for i:= tlistbox(source).items.count-1 downto 0 do
 begin
  if tlistbox(source).Selected[i] then
    begin
      with source as tlistbox do
      begin
        str:=items[i];
        items.Delete(i);
      end;
      with sender as tlistbox do
      begin
        items.Insert(itematpos(point(x,y),false),str);
      end;
    end;
  end;

end;
procedure TForm1.ListBox3DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  Accept := Source = ListBox3;
end;

代码解释:

1. `Button59Click` 过程:将 `ListBox3` 中的内容输出到 `output.txt` 文件中,然后使用记事本打开该文件。

2. `Button60Click` 过程:将 `Memo4` 中的内容清空,然后将 `Memo4` 中的每一行添加到 `ListBox3` 中。

3. `ListBox3DragDrop` 过程:处理 `ListBox3` 的拖放事件。当用户将一个或多个项目从一个 `ListBox` 拖放到另一个 `ListBox` 时,此过程将被调用。该过程将遍历源 `ListBox` 中的所有已选项目,并将其逐一添加到目标 `ListBox` 中。

4. `ListBox3DragOver` 过程:处理 `ListBox3` 的拖放事件。当用户将一个或多个项目从一个 `ListBox` 拖放到另一个 `ListBox` 时,此过程将被调用。

总结:

通过上述 Delphi 代码,我们实现了拖动排序功能,并将排序后的内容输出到文档中。这个示例代码可以帮助你了解 Delphi 中的拖放操作和文件输出操作的实现方式。你可以在 Delphi 的开发环境中运行该代码,并根据实际需求进行修改和扩展。

你可能感兴趣的:(delphi,dragdrop,拖拽,输出)