ATListbox

From Lazarus wiki
Jump to: navigation, search

About

ATListbox is OS-independant listbox control. It's made for dialog like Sublime Text's command list. Fully owner-drawn: you must not set captions, but must add OnDrawItem event, and set ItemCount, ItemIndex.

Listbox don't get focus by click; to get focus on click set CanGetFocus=true (if focused, it handles keys Up/Down/PgUp/PgDn/Home/End, else you need to handle OnKeyDown in form and set listbox's ItemIndex by hand, which is boring).

Author: Alexey Torgashin

Screenshot:

atlistbox.png

Download

Homepage at github: https://github.com/Alexey-T/ATButtons

License

MPL 2.0 or LGPL.

Requirements

Lazarus: 1.4.0

Tested on: Win32 (Windows 7), Linux GTK2 / QT (Ubuntu 14.04), macOS (10.8)

Properties

  • Items: it's just stringlist object that you can use or can ignore: it's ignored in code, listbox is virtual
  • ItemCount: number of items
  • ItemIndex: index of active item
  • ItemTop: index of item visible at the top
  • ItemHeight: height in pixels of each item
  • VisibleItems: count of items fully visible inside control
  • CanGetFocus: set to True to allow control to be focused on click

Events

  • OnDrawItem
  • OnClick
  • OnDblClick

Example

Let's create list:

var
  b: TATListbox;
 
procedure TfmMain.FormCreate(Sender: TObject);
begin
  b:= TATListbox.Create(Self);
  b.Parent:= Self;
  b.Align:= alClient;
 
  b.OnDrawItem:= @ListDraw;
  b.OnClick:= @ListClick;
 
  b.Color:= $e0e0e0;
  b.ItemCount:= 21;
end;

Let's set OnDrawItem to this (simply paint "itemNN"):

procedure TfmMain.ListDraw(Sender: TObject; C: TCanvas; AIndex: integer;
  const ARect: TRect);
begin
  C.Brush.Color:= IfThen(AIndex=b.ItemIndex, clMedGray, b.Color);
  C.FillRect(ARect);
 
  C.Pen.Color:= clMedGray;
  C.Line(ARect.Left+2, ARect.Bottom-1, ARect.Right-2, ARect.Bottom-1);
 
  C.TextOut(ARect.Left+6, ARect.Top+2, 'item '+inttostr(AIndex));
end;