Delphi TListBox OnClick / OnChange?

Is there a trick to getting the type of an OnChange function using a TListBox? I can subclass the component and add a property, etc., And then execute the OnClick code if the index changes ... I can also hack it using a form level variable to store the current index, but it's just interesting if I forget about obvious earlier I go anyway.

+7
delphi subclass onclick onchange tlistbox
source share
2 answers

It seems that there is nothing else but to implement it yourself. You need to remember the currently selected item and whenever the ItemIndex property changes from code or whenever the control receives an LBN_SELCHANGE notification (which currently fires the OnClick event), you will compare the index of the item you saved with the one specified in this moment by index of goods, and if they differ from each other, fire your own OnChange event. In the code of a nested class, this 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; 
+10
source share

with the OnClick event exactly the same ... you need to save the last value to compare it.

 if ListBox1.Items[ListBox1.ItemIndex]<> Edit1.Text then Edit1.Text := ListBox1.Items[ListBox1.ItemIndex]; 
0
source share

All Articles