As discussed in another thread I am writing a TChartListbox for being able to show/hide series of a TChart. In order to get notified for changes in the series I added a TListener to the component.
FListener := TListener.Create(@FChart, @SeriesChanged);
"SeriesChanged" presently just calls "Populate" which reads all series into the listbox:
procedure TChartListbox.Populate;
var
i : integer;
begin
try
FreeAndNil(FLegendItems);
Items.BeginUpdate;
Items.Clear;
if (FChart <> nil) then begin
FLegendItems := GetLegendItems;
for i:=0 to FLegendItems.Count-1 do begin
Items.AddObject('', FLegendItems[i]);
Checked[i] := TCustomChartSeries(FLegendItems[i].Owner).Active;
end;
end;
finally
Items.EndUpdate;
end;
end
There is a button on the form which adds a new series with random data to the chart.
procedure TForm1.Button1Click(Sender:TObject);
var
ser : TLineSeries;
begin
RandomChartSource.RandSeed := random(65000);
ser := TLineSeries.Create(Chart);
ser.Source := RandomChartSource;
ser.SeriesColor := rgbToColor(random(255), random(256), random(256));
ser.Title := Format('Series %d', [Chart.SeriesCount]);
Chart.AddSeries(ser);
end;
The new series appeas in the chart immediately. In the listbox, however, it appears only after clicking the button for a second time. There must be something in the listening/broadcasting mechanism which I do not understand.
What am I doing wrong?