I'm currently trying to add a feature in my application where the user can select a VCL style to there preference. I could manually add all the styles into the ComboBox directly but i'm sure there's a much easier way.
Asked
Active
Viewed 407 times
1 Answers
3
Create a new C++Builder VCL application. In the Project | Options | Application | Appearance menu choose some Custom Style Names.
Then add Button and ComboBox box components to your C++ VCL form. For the Button's onlick and ComboBox's Change events use the following code. You'll also need to put #include near the top of the source code for the form :D Compile and run, click the button to see the combobox populated with the styles you selected in the project option for appearance. Then select one of the styles from the combobox to change the style of the app.
I've tested this code with RAD Studio 10.4 Sydney. Should work for any recent release of C++Builder.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ComboBox1->Items->BeginUpdate();
try
{
ComboBox1->Items->Clear();
DynamicArray<String> styleNames = Vcl::Themes::TStyleManager::StyleNames;
for(int i = 0; i < styleNames.Length; ++i)
{
String styleName = styleNames[i];
ComboBox1->Items->Add(styleName);
}
}
__finally
{
ComboBox1->Items->EndUpdate();
}
}
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
// set the style for the selected combobox item
Vcl::Themes::TStyleManager::TrySetStyle(ComboBox1->Items->Strings[ComboBox1->ItemIndex],false);
}

user186876
- 191
- 5
-
This answers only half of the OP's question. This shows how to retrieve the styles and put them into a ComboBox, but not how to apply the selected style each time the user selects an item in the ComboBox. That would involve calling [`TStyleManager::(Try)SetStyle()`](http://docwiki.embarcadero.com/Libraries/en/Vcl.Themes.TStyleManager.SetStyle) in the `TComboBox::OnSelect` event. – Remy Lebeau Jun 11 '20 at 18:45
-
I will update the code with the OnSelect event handler. Sorry for not seeing (or re-reading the question) to get the second part of the request – user186876 Jun 11 '20 at 20:39
-
I updated my answer above to include the ComboBox OnChange event handler code - thanks Remy – user186876 Jun 11 '20 at 21:02