现在使用控件, 更喜欢继承
2021-05-01 00:29
826 查看
现在使用控件, 更喜欢继承
以前写代码, 总是把主单元弄得满满当当; 现在更喜欢把控件比较独立的功能写成一个单元, 改写属性、重载方法...哪怕只有一点点和默认不同, 也喜欢独立出来.
刚刚用到 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;
相关文章推荐
- 写了一个字符串的二维表: TSta
- 覆盖、再覆盖
- 记一次失败的Perl + Nginx + FastCGI 配置过程
- 执行 Application.Terminate 后, OnDestroy 中的代码还会执行
- Perl Dancer 学习(一)
- 关于字符串, 之前没这样用过
- 以接口为主导的设计中, 我在使用的框架模式
- 结合领域驱动设计的SOA分布式软件架构
- 获取程序自身大小的函数
- 捕捉 midi 输入消息的基本程序
- Delphi XE2 之 FireMonkey 入门(9) - TBitmap
- C#综合揭秘——细说进程、应用程序域与上下文之间的关系C#综合揭秘——细说多线程(下)”
- WCF揭秘——共享数据契约
- 使用泛型, 写一个为任意类型的动态数组添加元素的方法
- 准备理一下菜单和工具栏相关的组件
- 发现 TSplitter 在嵌套时不好用, 索性写了个替代品
- Action 相关组件
- WF工作流与Web服务的相互调用 —— WF与WCF互相调用(利用ReceiveActivity把WF发布为WCF)
- WF工作流与Web服务的相互调用 —— 以InvokeWebServiceActivity在Workflow工作流调用Web服务
- WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(开发持久化工作流)