欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

现在使用控件, 更喜欢继承(覆盖控件已有的函数,很奇怪的一种使用方式)...

发布时间:2025/5/22 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 现在使用控件, 更喜欢继承(覆盖控件已有的函数,很奇怪的一种使用方式)... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

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


 

unit ListBox2;interfaceusesSystem.Classes, Vcl.Controls, Vcl.StdCtrls, System.Types;typeTListBox2 = class(TCustomListBox)protectedprocedure DragOver(Source: TObject; X: Integer; Y: Integer; State: TDragState; var Accept: Boolean); override;procedure DblClick; override;publicconstructor Create(AOwner: TComponent); override;procedure DragDrop(Source: TObject; X: Integer; Y: Integer); override;end;implementation{ TListBox2 }constructor TListBox2.Create(AOwner: TComponent); begininherited;DragMode := dmAutomatic; end;procedure TListBox2.DblClick; begininherited;Items.Delete(ItemIndex); end;procedure TListBox2.DragDrop(Source: TObject; X, Y: Integer); begininherited;Items.Exchange(ItemIndex, ItemAtPos(Point(X,Y), True)); end;procedure TListBox2.DragOver(Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begininherited;Accept := True; end;end.


测试:


 

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

转载于:https://www.cnblogs.com/cyzgg/p/10719459.html

总结

以上是生活随笔为你收集整理的现在使用控件, 更喜欢继承(覆盖控件已有的函数,很奇怪的一种使用方式)...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。