Hola,
Estoy intentando crear un evento que detecte un cambio en un ListBox y me actualice una etiqueta. Este es el código que tengo, compila bien pero al ejecutarlo me lanza una excepción diciendo que no encuentra la clase TListBox:
unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Windows; type { TChListBox } TChListBox = class(TListBox) private FOnItemCountChange: TNotifyEvent; protected procedure WndProc(var Message: TMessage); override; published property OnItemCountChange: TNotifyEvent read FOnItemCountChange write FOnItemCountChange; end; { TForm1 } TForm1 = class(TForm) Button1: TButton; Label1: TLabel; ListBox1: TChListBox; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure OnChangeListBox(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin ListBox1 := TChListBox.Create(Self); ListBox1.Parent := Self; ListBox1.OnItemCountChange := @OnChangeListBox; end; procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items.Add('Elemento'); end; procedure TForm1.OnChangeListBox(Sender: TObject); begin Label1.Caption:= 'Número de elementos: '+ IntToStr(ListBox1.Count); end; procedure TChListBox.WndProc(var Message: TMessage); var OldCount: Integer; begin case Message.Msg of LB_ADDSTRING, LB_INSERTSTRING, LB_DELETESTRING: begin // for LB_(ADD|INSERT)STRING, Message.Result is the 0-based // index of the added string, or a LB_ERR... error code. // // for LB_DELETESTRING, Message.Result is the number of items // remaining in the list, or a LB_ERR... error code. // inherited; if (Message.Result >= 0) and Assigned(FOnItemCountChange) then FOnItemCountChange(Self); end; LB_RESETCONTENT: begin // the Message.Result is not used in this message. // OldCount := Items.Count; inherited; if (OldCount <> Items.Count) and Assigned(FOnItemCountChange) then FOnItemCountChange(Self); end; else inherited; end; end; end.