現在使用控件, 更喜歡繼承



以前寫代碼, 總是把主單元弄得滿滿當當; 現在更喜歡把控件比較獨立的功能寫成一個單元, 改寫屬性、重載方法...哪怕只有一點點和默認不同, 也喜歡獨立出來.

剛剛用到 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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM