我想制作一个基于TListBox
.
当Items.Count
更改时,我想更新Caption
a TLabel
:
Label1.Caption := IntToStr(ListBox1.Items.Count);
我制作了下面的组件,但它不起作用:
unit UChangeListBox;
interface
uses
Classes, StdCtrls, Messages;
type
TChListBox = class(StdCtrls.TListBox)
private
FItemIndex: Integer;
FOnChange: TNotifyEvent;
procedure CNCommand(var AMessage: TWMCommand); message CN_COMMAND;
protected
procedure DoChange; virtual;
procedure SetItemIndex(const Value: Integer); override;
published
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
procedure Register;
implementation
procedure TChListBox.DoChange;
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TChListBox.CNCommand(var AMessage: TWMCommand);
begin
inherited;
if (AMessage.NotifyCode = LBN_SELCHANGE) and (FItemIndex <> ItemIndex) then
begin
FItemIndex := ItemIndex;
DoChange;
end;
end;
procedure TChListBox.SetItemIndex(const Value: Integer);
begin
inherited;
if FItemIndex <> ItemIndex then
begin
FItemIndex := Value;
DoChange;
end;
end;
procedure Register;
begin
RegisterComponents('MyComponents',[TChListBox]);
end;
end.