随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


以前写代码, 总是把主单元弄得满满当当; 现在更喜欢把控件比较独立的功能写成一个单元, 改写属性、重载方法...哪怕只有一点点和默认不同, 也喜欢独立出来.

刚刚用到 TListBox, 需要能拖动元素、双击删除.

unit ListBox2;

interface

uses
  System.Classes, Vcl.Controls, Vcl.StdCtrls, System.Types;

type
  TListBox2 = class(TCustomListBox)
  protected
    procedure DragOver(Source: TObject; X: Integer; Y: Integer; State: TDragState; var Accept: Boolean); override;
    procedure DblClick; override;
  public
    constructor Create(AOwner: TComponent); override;
    procedure DragDrop(Source: TObject; X: Integer; Y: Integer); override;
  end;

implementation

{ TListBox2 }

constructor TListBox2.Create(AOwner: TComponent);
begin
  inherited;
  DragMode := dmAutomatic;
end;

procedure TListBox2.DblClick;
begin
  inherited;
  Items.Delete(ItemIndex);
end;

procedure TListBox2.DragDrop(Source: TObject; X, Y: Integer);
begin
  inherited;
  Items.Exchange(ItemIndex, ItemAtPos(Point(X,Y), True));
end;

procedure TListBox2.DragOver(Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  inherited;
  Accept := True;
end;

end.


测试:

uses ListBox2;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TListBox2.Create(Self) do begin
    Parent := Self;
    Align := alLeft;
    Items.CommaText := 'A,B,C,D,E,F,G';
  end;
end;

posted on   万一  阅读(5224)  评论(15编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧


点击右上角即可分享
微信分享提示