以前写代码, 总是把主单元弄得满满当当; 现在更喜欢把控件比较独立的功能写成一个单元, 改写属性、 重载方法...哪怕只有一点点和默认不同, 也喜欢独立出来. 刚刚用到 TListBox, 需要能拖动元素、双击删除.
unit ListBox2; interfaceuses System.Classes, Vcl.Controls, Vcl.StdCtrls, System.Types; type TListBox2 = 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); 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) dobeginParent := Self; Align := alLeft; Items.CommaText := 'A,B,C,D,E,F,G'; end; end;
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索integer
, override
, source
, end
, procedure
, DragDrop
, Inherited
dblclick
delphi 继承、delphi 继承基类窗体、delphi 窗体继承、delphi 接口继承、delphi 多继承,以便于您获取更多的相关知识。
时间: 2024-10-31 10:49:16