how can i delete one control based on its tagstring ?

dolphin King
dolphin King used Ask the Experts™
on
I had this question after viewing Tvertscrollbox like a whatsapp layout delete messages.

this answer shows how to delete all CR := TCalloutRectangle inside Vertscrollbox

i wanted to delete only one TCalloutRectangle based on its tag string how can i do that ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software architect
Top Expert 2012
Commented:
My example set Tag property of . So to delete by Tab I make function ...
...
  CR := TCalloutRectangle.Create(VSB);
...
  CR.Tag := iTagID;
...


function DeleteCallout(sb: TVertScrollBox; idx: Integer): Boolean;
  var
  i: Integer;
  obj: TFMXObject;
begin
  Result := False;
  //go throug all childs
  for i := 0 to sb.Content.ChildrenCount-1 do
  begin
    //is specific object class
    if sb.Content.Children.Items[i] is TCalloutRectangle then
    begin
      //find it by Tag
      if (sb.Content.Children.Items[i] as TCalloutRectangle).Tag = idx then
      begin
        obj := (sb.Content.Children.Items[i] as TCalloutRectangle);
        sb.Content.RemoveObject(obj); //delete from parent
        Result := True;
        Break;
      end;
    end;
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  DeleteCallout(VSB, 1);
end;

Open in new window

... but you can check for string content (of child component) too.... I don't know what your tag string is ...

Author

Commented:
why when i delete some times app crashes ?
Sinisa VukSoftware architect
Top Expert 2012

Commented:
Is it on windows or android? Can you catch exception (try .. except ... showmessage .. end)?
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
it is on android even try block did not catch any exception

Author

Commented:
i believe this was fmx problem it self
Geert GOracle dba
Top Expert 2009

Commented:
when you delete something always use a reverse loop ...

  for i := count-1 downto 0 do 
    // delete stuff

Open in new window


otherwise the counter goes beyond the end point

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial