Recent

Author Topic: [SOLVED] Catching error on SVGButton  (Read 1247 times)

AsleyCruz

  • Full Member
  • ***
  • Posts: 107
    • Graphic and web designer
[SOLVED] Catching error on SVGButton
« on: August 22, 2024, 03:30:00 pm »
Hi there,
How can I catch the error if inserted wrong SVG string on SVGButton?

I am trying to create a component and if I insert wrong SVG string,
Lazarus freezes and crash. See attached image

Code: Pascal  [Select][+][-]
  1. //Code on the component
  2. try
  3.   BCSVGButton1.SVGNormalXML.Text:=Memo1.Text;
  4.   BCSVGButton1.RedrawBitmap;
  5. except
  6.   Dialogs.MessageDlg('Icon','Unable to load the icon from SVG string.', mtError, [mbOk], 0, mbOk);
  7. end;

Thanks a lot!
« Last Edit: August 22, 2024, 04:58:28 pm by AsleyCruz »
Graphic & web designer

paweld

  • Hero Member
  • *****
  • Posts: 1213
Re: Catching error on SVGButton
« Reply #1 on: August 22, 2024, 04:04:20 pm »
svg is simply xml, only with specific tags. you can do it like this:
Code: Pascal  [Select][+][-]
  1. uses
  2.   DOM, XMLRead;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   Doc: TXMLDocument;
  7.   ss: TStringStream;
  8. begin
  9.   ss := TStringStream.Create(memo1.Lines.Text);
  10.   Doc := TXMLDocument.Create;
  11.   try
  12.     ReadXMLFile(Doc, ss);
  13.     ShowMessage('XML / SVG OK!');
  14.   except
  15.     on E: Exception do
  16.       ShowMessage('XML / SVG Error: ' + E.Message);
  17.   end;
  18.   Doc.Free;
  19.   ss.Free;
  20. end;    
  21.  
Best regards / Pozdrawiam
paweld

AsleyCruz

  • Full Member
  • ***
  • Posts: 107
    • Graphic and web designer
Re: Catching error on SVGButton
« Reply #2 on: August 22, 2024, 04:59:09 pm »
svg is simply xml, only with specific tags. you can do it like this...

Thanks a lot, it worked.

This is my function:

Code: Pascal  [Select][+][-]
  1. function IsSVGValid(aText:String):boolean;
  2. var
  3.   Doc: TXMLDocument;
  4.   SS: TStringStream;
  5. begin
  6.   SS := TStringStream.Create(aText);
  7.   Doc := TXMLDocument.Create;
  8.   try
  9.     ReadXMLFile(Doc, SS);
  10.     Result:=true;
  11.   except
  12.     Result:=false;
  13.   end;
  14.   Doc.Free;
  15.   SS.Free;
  16. end;
  17.  
  18. if IsSVGValid(Memo1.Text) then begin
  19.   BCSVGButton1.SVGNormalXML.Text:=Memo1.Text;
  20.   BCSVGButton1.RedrawBitmap;
  21. end else
  22.   Dialogs.MessageDlg('Icon','Unable to load the icon from SVG string.', mtError, [mbOk], 0, mbOk);
Graphic & web designer

 

TinyPortal © 2005-2018