4

It's cool that Delphi XE3 and below have styles for our applications. But I've noticed that we can mark as many styles as we want and them choose which of them to use as default.

It means that we could change styles at will, but how to do it in code? And how to let the user choose which style to use in our software?

Ken White
  • 123,280
  • 14
  • 225
  • 444
NaN
  • 8,596
  • 20
  • 79
  • 153

1 Answers1

12

TStyleManager does what you need to accomplish this task. Use TStyleManager.StyleNames to get a list of the styles, and TStyleManager.TrySetStyle to change them at runtime.

To see how this works, start a new VCL Forms Application. Add all of the VCL styles you want to the project, and drop a TComboBox on a form. You'll need to add the implementation uses clause like I have below:

uses
  Vcl.Themes;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  TStyleManager.TrySetStyle(ComboBox1.Items[ComboBox1.ItemIndex]);
end;

procedure TForm1.FormShow(Sender: TObject);
var
  s: String;
begin
  ComboBox1.Items.BeginUpdate;
  try
    ComboBox1.Items.Clear;
    for s in TStyleManager.StyleNames do
       ComboBox1.Items.Add(s);
    ComboBox1.Sorted := True;
    // Select the style that's currently in use in the combobox
    ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(TStyleManager.ActiveStyle.Name);
  finally
    ComboBox1.Items.EndUpdate;
  end;
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    How do you know so much? – NaN Apr 11 '13 at 02:07
  • 9
    I read the VCL source code, the documentation that's available, lots of StackOverflow posts (even ones I know nothing about that just look interesting), and write lots of code to try to figure out different things. :-) – Ken White Apr 11 '13 at 02:14
  • @EASI Very often we don't know the answer and just do a web search to find out. That's how I located the dupe of this question. – David Heffernan Apr 11 '13 at 13:29
  • @DavidHeffernan. After using `TStyleManager.TrySetStyle(ComboBox1.Items[ComboBox1.ItemIndex]);` Modal window become non Modal? DX10. – Zam Nov 08 '15 at 09:06