zoukankan      html  css  js  c++  java
  • 现在使用控件, 更喜欢继承

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

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

     

  • 相关阅读:
    JavaScript大杂烩1
    JavaScript大杂烩0
    Scrum敏捷开发沉思录
    C#的变迁史
    C#的变迁史
    C#的变迁史
    巧用浏览器F12调试器定位系统前后端bug-转载
    Jmeter如何把响应数据的结果保存到本地的一个文件
    Jmeter察看结果树的响应数据中的中文显示乱码问题处理
    Jmeter如何将上一个请求的结果作为下一个请求的参数——使用正则表达式提取器转载
  • 原文地址:https://www.cnblogs.com/karkash/p/3380172.html
Copyright © 2011-2022 走看看