德尔福TListBox OnClick/OnChange吗? [英] Delphi TListBox OnClick / OnChange?
问题描述
使用TListBox获得"OnChange"类型的功能是否有用?我可以对组件进行子类化并添加一个属性,等等,然后仅在索引发生更改时才执行OnClick代码...我还可以使用表单级变量对其进行修改以存储当前索引,但是只是想知道我之前是否忽略了显而易见的内容我走另一条路.
Is there a trick to getting "OnChange" type of functionality with a TListBox? I can subclass the component and add a property, etc then only carry out OnClick code if the Index changes... I can also hack it with a form level variable to store the current index but just wondering if I'm overlooking the obvious before I go one way or the other.
推荐答案
除了自己实现,似乎别无选择.您需要记住当前选定的项目,并且每当 ItemIndex
属性会从代码中更改,或者只要控件收到 LBN_SELCHANGE
通知(当前会触发
There seems to be no way other than implementing this by yourself. What you need is to remember the currently selected item and whenever the ItemIndex
property is changed from code or whenever the control receives the LBN_SELCHANGE
notification (which currently fires the OnClick
event), you will compare the item index you stored with item index which is currently selected and if they differ, fire your own OnChange
event. In code of an interposed class it could be:
type
TListBox = class(StdCtrls.TListBox)
private
FItemIndex: Integer;
FOnChange: TNotifyEvent;
procedure CNCommand(var AMessage: TWMCommand); message CN_COMMAND;
protected
procedure Change; virtual;
procedure SetItemIndex(const Value: Integer); override;
published
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
implementation
{ TListBox }
procedure TListBox.Change;
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TListBox.CNCommand(var AMessage: TWMCommand);
begin
inherited;
if (AMessage.NotifyCode = LBN_SELCHANGE) and (FItemIndex <> ItemIndex) then
begin
FItemIndex := ItemIndex;
Change;
end;
end;
procedure TListBox.SetItemIndex(const Value: Integer);
begin
inherited;
if FItemIndex <> ItemIndex then
begin
FItemIndex := ItemIndex;
Change;
end;
end;
这篇关于德尔福TListBox OnClick/OnChange吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!