Remove components at runtime in Delphi with Android

Asked

Viewed 1,774 times

1

When creating a Tbutton dynamically at runtime within a Tvertscrollbox. When I need to delete this button it does not disappear from the screen. This problem only occurs by running the application on Android (on Windows works properly).

To delete the buttons created at runtime within Tvertscrollbox I run this routine:

   for i := vsScroll.ComponentCount-1 downto 0 do
      if vsScroll.Components[i] is TButton then
         TButton(vsScroll.Components[i]).Free;

How do I remove created components?

Note: No use to use TButton(vsScroll.Components[i]).Destroy instead of Free.

  • This way I believe that before the end of the "loop" will generate an error, as the component count has already been decreased with each removal.

  • It wouldn’t be viable just to make the object invisible instead of eliminating it? TButton(vsScroll.Components[i]).Visible := false;

  • @Carlosandrade, no error at the end of the loop (the normal way yes, will give error... can try). About making the component invisible, is not feasible. They have to be eliminated. After all the function Free has this goal and as a consequence, by eliminating the object, it becomes invisible.

1 answer

1


Already solved. For Android the release function of the memory component and consequent screen deletion is the DisposeOf and not Free, as I used to use in VCL components.

Help from Delphi says the following:

Disposeof forces the Execution of the destructor code in an Object. The new Delphi mobile compilers introduce a new Dispose Pattern implemented by Calling Disposeof, which executes the destructor code Even if there are variables with pending References to the Object. After the Disposeof method is called, the Object is placed in a special state, the Disposed state. This Means that the destructor is not called Again if Disposeof is called Again, or if the Reference Count Reaches zero (the Moment in which the memory is Released).

The routine of deleting the components from my list created at runtime was like this:

   for i := vsScroll.ComponentCount-1 downto 0 do
      if vsScroll.Components[i] is TButton then
         TButton(vsScroll.Components[i]).DisposeOf;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.