Hi Guys and girls,

I'm trying to save the font settings of my component and load them back in via xml.

I have xml working with saving and loading of every property except Font.

When I save font its as an integer.

But when I load the value back in the same way it does access violation.

I'm using the same way of changing the font at run time but when it comes to loading the same details it fails.

Any help is much appreciated.

Thanks in Advance

Changing my Font at run time this works

if ValueListEditor1.Keys[I] = 'Font' then
    begin
        selectedControl := DesignCanvas.getSelectedControl;
        TempFont := TFont(GetOrdProp(selectedControl, 'Font'));
        if assigned(TempFont) then
        begin
           TempFont.Color := TFont(GetOrdProp(selectedControl, 'Font')).Color;
        end;
      FontDialog1.Font := tempFont;
      FontDialog1.Execute;
      SetOrdProp(SelectedControl,'Font', Integer(FontDialog1.Font));
    end

Loading Font from XML thats not working but using the same methods as above.

tempInt := 0;
  tempFont := nil;

      tempInt := Integer(String(Node.Attributes['Font']));
      tempFont := TFont(tempInt);
      SetOrdProp(component,'Font', Integer(tempFont));
const
  csfsBold      = '|Bold';
  csfsItalic    = '|Italic';
  csfsUnderline = '|Underline';
  csfsStrikeout = '|Strikeout';

procedure StringToFont(
  sFont : string; Font : TFont );
var
  p      : integer;
  sStyle : string;
begin
  with Font do
  begin
    // get font name
    p    := Pos( ',', sFont );
    Name :=
      Copy( sFont, 2, p-3 );
    Delete( sFont, 1, p );

    // get font size
    p    := Pos( ',', sFont );
    Size :=
      StrToInt( Copy( sFont, 2, p-2 ) );
    Delete( sFont, 1, p );

    // get font style
    p      := Pos( ',', sFont );
    sStyle :=
      '|' + Copy( sFont, 3, p-4 );
    Delete( sFont, 1, p );

    // get font color
    Color :=
      StringToColor(
        Copy( sFont, 3,
          Length( sFont ) - 3 ) );

    // convert str font style to
    // font style
    Style := [];

    if( Pos( csfsBold,
          sStyle ) > 0 )then
      Style := Style + [ fsBold ];

    if( Pos( csfsItalic,
          sStyle ) > 0 )then
      Style := Style + [ fsItalic ];

    if( Pos( csfsUnderline,
          sStyle ) > 0 )then
      Style := Style + [ fsUnderline ];

    if( Pos( csfsStrikeout,
          sStyle ) > 0 )then
      Style := Style + [ fsStrikeout ];
  end;
end;

//
// Output format:
//   "Aril", 9, [Bold|Italic], [clAqua]
//

procedure DoSomething;
var
   Node : IXMLNode;
begin
   Node := XMLDocument1.DocumentElement.ChildNodes.FindNode('config');
   if (node <> nil) then
   begin
      ComponentX.Font := StrToFont(Node.ChildNodes['font'].text));
   end;
end;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.