IceCold
IceCold

Reputation: 21212

How to check if a style file is already loaded?

I have a ListBox that lists all style files (vsf) in a folder. When the user clicks a file, I load that style:

 if TStyleManager.IsValidStyle(sSkinFile, StyleInfo) then
  begin
   TStyleManager.LoadFromFile(sSkinFile);
   TStyleManager.SetStyle(StyleInfo.Name);
  end

However, if the user clicks a style that was already loaded (was clicked previously), Delphi will rise and exception: "Style 'Golden Graphite' already registered".

Note: It looks like the system will not release the previous styles when a new style is loaded. I think that the memory consumption will be a bit higher if the user will start clicking all listed styles.

How do I check if a style was already loaded?

Upvotes: 3

Views: 905

Answers (3)

IceCold
IceCold

Reputation: 21212

Something weird I discovered today: TStyleManager.IsValidStyle always fails if Vcl.Styles is not in the USES list!!


SOLUTION: simply add Vcl.Styles to the Uses list.

Upvotes: 0

RRUZ
RRUZ

Reputation: 136431

You can use the Style property of the TStyleManager, this property will return nil when a VCL Style is not loaded. Try this sample.

uses
  Vcl.Styles,
  Vcl.Themes;

function  VCLStyleLoaded(StyleName : string) : Boolean;
begin
 Result := TStyleManager.Style[StyleName] <> nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 StyleInfo: TStyleInfo;
begin
  if OpenDialog1.Execute then
  begin        
    if TStyleManager.IsValidStyle(OpenDialog1.FileName, StyleInfo)  and not VCLStyleLoaded(StyleInfo.Name) then
    begin
     TStyleManager.LoadFromFile(OpenDialog1.FileName);
     TStyleManager.SetStyle(StyleInfo.Name);
    end
  end;
end;

Upvotes: 3

Uwe Raabe
Uwe Raabe

Reputation: 47819

You can call TStyleManager.TrySetStyle and load the style only when it fails.

Upvotes: 2

Related Questions