Search code examples
firemonkeyc++builderteechart

How to delete TChart (FMX, C++)


I can't figure out how to delete a TChart. This has to be simple, but I'm not seeing it. In the TListView ButtonClick code below, I delete a series from the active TChart and, if the series happens to be the last one, then I want to delete the entire TChart.

TComponent *T;                     // find chart and delete the selected series
for (int i = 0; i < (Form1->ComponentCount); i++) {
   T = Form1->Components[i];
   if (T->ClassName() == "TChart") {
      int test = T->Tag;
      if (test == TappedChartTag) { //TappedChartTag is global
        if (TChart *TC = dynamic_cast<TChart *>(T)) {
            TC->RemoveSeries(AItem->Index); // this removes the series
            if (TC->SeriesCount()==0) {  // if this was last series, delete chart
               TC->CleanupInstance();
            }
        }
      }
    }
}

I can't find a way to completely get rid of the TChart. The TC->CleanupInstance(); wipes it completely (nothing visible), but the TChart is still there.

I'm working in Embarcadero Rad Studio (C++ Builder 10.3 Update 1).

UPDATE: I tried delete TC; in place of that TC->CleanupInstance(); and that works on Win32 but not on iOS.


Solution

  • Ok, based on the solution Remy provides in this question I've got it working now. I just replaced the TC->CleanupInstance(); with a line to set an integer equal to the components index, delINT = i. Then below the loop that iterates through the components i put this simple code:

    if (delINT > 0) {
      Form1->Components[delINT]->DisposeOf();
    }
    

    I was making things much harder than should have been. I couldn't call DisposeOf() in my earlier code because it was out of context. In this case, it works fine (on Win32, iOS, and Android).