ListBox - fazer uma que permite editar os itens

Top  Previous  Next

unit EditListBox; 

 

interface 

 

uses 

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 

  StdCtrls; 

 

type 

  TEditListBox = class(TCustomListBox) 

  ListEdit : TEdit; 

  private 

    { Private declarations } 

  protected 

    { Protected declarations } 

    procedure DblClick; override; 

    procedure KeyDown(var Key: Word; Shift: TShiftState); override; 

  public 

    { Public declarations } 

    constructor Create(AOwner : TComponent); override; 

    destructor Destroy; override; 

    procedure EditExit(Sender : TObject); 

    procedure EditKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState); 

    procedure EditKeyPress(Sender: TObject; var Key: Char); 

  published 

    { Published declarations } 

    property Align; 

    // 

    // put here the properties of TCustomListBox 

    // 

    property OnStartDrag; 

  end

 

procedure Register; 

 

implementation 

 

procedure Register; 

begin 

  RegisterComponents('Additional', [TEditListBox]); 

end

 

{ TEditListBox } 

 

constructor TEditListBox.Create(AOwner: TComponent); 

begin 

  inherited; 

  ListEdit := TEdit.Create(Self); 

  with ListEdit do 

   begin 

     Parent := Self; 

     Visible := False; 

     Left := 0

     Color := clAqua; 

     BorderStyle := bsNone; 

     OnExit := EditExit; 

     OnKeyPress := EditKeyPress; 

     OnKeyDown := EditKeyDown; 

   end

 

end

 

procedure TEditListBox.DblClick; 

begin 

  inherited; 

  with Self do 

    begin 

      ListEdit.Height  := ItemHeight; 

      ListEdit.Width   := ClientWidth; 

      ListEdit.Visible := True; 

      ListEdit.Text    := Items[ItemIndex]; 

      ListEdit.Top     := (ItemIndex*ItemHeight)-(ItemHeight*TopIndex); 

      ListEdit.Tag     := ItemIndex; 

      ListEdit.SetFocus; 

    end

end

 

destructor TEditListBox.Destroy; 

begin 

  ListEdit.Free; 

  inherited; 

end

 

procedure TEditListBox.EditExit(Sender: TObject); 

begin 

  Self.Items[ListEdit.Tag]:= ListEdit.Text; 

  ListEdit.Visible:=False; 

end

 

procedure TEditListBox.EditKeyDown; 

begin 

  if Key = vk_return then 

    begin 

      Key := vk_tab ; 

      Self.SetFocus; 

    end

end

 

procedure TEditListBox.EditKeyPress; 

begin 

  if Key = #13 then 

     key := #0

end

 

procedure TEditListBox.KeyDown; 

begin 

  if key = vk_return then 

    begin 

      DblClick; 

    end

  inherited; 

end

 

end