Skip to content

Commit b4e2ac2

Browse files
authored
Merge pull request #710 from telerik/nkaraiva/kb-custom-theme-as-default
New KB: How to Use a Custom Theme as Default for the Entire Application
2 parents 65d02a6 + 141b985 commit b4e2ac2

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: How to Use a Custom Theme as Default for the Entire Application
3+
description: Learn how to use a custom defined theme as default for the Entire Application
4+
type: how-to
5+
page_title: How to Use a Custom Theme as Default for the Entire Application
6+
slug: custom-theme-as-default
7+
tags: common, theme, default, application, winforms, telerik
8+
res_type: kb
9+
---
10+
11+
## Environment
12+
13+
|Product Version|Product|Author|
14+
|----|----|----|
15+
|2025.1.211|Theme Tutorials for WinForms|[Nadya Todorova](https://www.telerik.com/blogs/author/nadya-karaivanova)|
16+
17+
## Description
18+
19+
Telerik Presentation Framework allows users to customize any pre-defined themes that Telerik UI for WinForms offers. We provide an opportunity thanks to [Visual Style Builder]({%slug winforms/tools/visual-style-builder%}) tool to customize the appearance of our controls to achieve the desired style for your application.
20+
21+
Let's pretend, we already have applied customizations and there is a custom theme already setup. For the perpose of this example, I prepared my custom **Windows11CompactLightBlue** theme which overrides the **Windows11Compact** theme. I generate variation by using [Windows11CompactTheme Blending]({%slug winforms/tools/visual-style-builder/working-with-visual-style-builder/windows11-themes-blending%}) feature. I created a theme variation and use LightBlue accent color to provide my theme new fresh look. Now, it is time to start using my custom theme.
22+
23+
This tutorial demonstrates how to use the newly created Windows11CompactLightBlue theme as default theme in my application.
24+
25+
>caption Figure 1: Using custom theme as default theme in the application:
26+
27+
![custom-theme-as-default003](images/custom-theme-as-default003.png)
28+
29+
## Solution
30+
31+
First, it would be necessary to create theme a component. Following the instructions provided in [Creating a theme component](https://docs.telerik.com/devtools/winforms/styling-and-appearance/advanced-topics/adding-custom-redistributable-themes-to-your-application/creating-a-theme-component) article, we should:
32+
33+
1. Add a new Class Library project to the current solution.
34+
35+
2. Add Windows11CompactLightBlue.tssp file into the Library project. It is important to set the **Build Action** of the .tssp to *Embedded Resource*.
36+
37+
3. Add a new Windows11LightBlue class to represent my theme component to the project.
38+
39+
40+
````C#
41+
42+
namespace Windows11LightBlueClassLibrary
43+
{
44+
public class Windows11LightBlue : RadThemeComponentBase
45+
{
46+
static bool loaded;
47+
public Windows11LightBlue()
48+
{
49+
ThemeRepository.RegisterTheme(this);
50+
}
51+
public override void Load()
52+
{
53+
if (!loaded || this.IsDesignMode)
54+
{
55+
loaded = true;
56+
Assembly resourceAssembly = typeof(Windows11LightBlue).Assembly;
57+
this.LoadResource(resourceAssembly, "Windows11LightBlueClassLibrary.Windows11CompactLightBlue.tssp");
58+
}
59+
}
60+
public override string ThemeName
61+
{
62+
get { return "Windows11CompactLightBlue"; }
63+
}
64+
}
65+
}
66+
67+
68+
````
69+
70+
>caption Figure 1: Here is how my ClassLibrary looks:
71+
72+
![custom-theme-as-default001](images/custom-theme-as-default001.png)
73+
74+
4. Once the Windows11LightBlueClassLibrary gets ready, we should define the custom Windows11CompactLightBlue theme as default in the entire application. This can be done inside the App.config file of our application. For more information, follow the instrucions defined here: [Custom theme as Default](https://docs.telerik.com/devtools/winforms/styling-and-appearance/default-theme#custom-theme-as-default)
75+
76+
````XML
77+
78+
<?xml version="1.0" encoding="utf-8" ?>
79+
<configuration>
80+
<startup>
81+
</startup>
82+
<appSettings>
83+
<!--define the custom Windows11CompactLightBlue for the entire application-->
84+
<add key="TelerikWinFormsThemeName" value="Windows11CompactLightBlue" />
85+
<add key="TelerikWinFormsThemeType" value="Windows11LightBlueClassLibrary.Windows11LightBlue"/>
86+
<add key="TelerikWinFormsThemeAssemblyName" value="Windows11LightBlueClassLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
87+
</appSettings>
88+
</configuration>
89+
90+
````
91+
92+
>important It is important to specify the exact TelerikWinFormsThemeAssemblyName, Version, Culture and PublicKeyToken values. These values defined in the app.config should strictly match the same values from the custom theme assembly in order to make sure that the theme should be correctly applied at design time as well as at run time.
93+
94+
5. Build the whole solution. Now, we can find our Windows11CompactLightBlue theme in the VS Toolbox at design time and simply drag-drop it onto RadForm as any other component. You can see the Windows11LightBlue instance in the compoenent tray. From now on, any Telerik control that you add to RadForm would have the custom theme applied automatically.
95+
96+
![custom-theme-as-default002](images/custom-theme-as-default002.png)
97+
98+
6. If you do not want to add the theme at design time, you can create an instance of the theme in the start up of the application, for example in Program.cs:
99+
100+
````C#
101+
102+
internal static class Program
103+
{
104+
/// <summary>
105+
/// The main entry point for the application.
106+
/// </summary>
107+
[STAThread]
108+
static void Main()
109+
{
110+
new Windows11LightBlueClassLibrary.Windows11LightBlue();
111+
Application.EnableVisualStyles();
112+
Application.SetCompatibleTextRenderingDefault(false);
113+
Application.Run(new RadForm1());
114+
}
115+
}
116+
117+
````
118+
119+
We are finished :partying_face:! The Windows11CompactLightBlue theme is now used as default for entire application both at design time and run time.
120+
121+
> [!TIP]
122+
> Complete projects in .NET9 and NET Framework 48 are available in our SDK repo [here](https://github.com/telerik/winforms-sdk/tree/master/Themes/CustomThemeAsDefault).
123+
124+
125+
## See Also
126+
127+
- [Using a default theme for the entire application](https://docs.telerik.com/devtools/winforms/styling-and-appearance/using-a-default-theme-for-the-entire-application#using-a-default-theme-for-the-entire-application)
128+
- [Using custom themes](https://docs.telerik.com/devtools/winforms/styling-and-appearance/using-custom-themes)
35.4 KB
Loading
22.7 KB
Loading
10.9 KB
Loading

0 commit comments

Comments
 (0)