Telerik blogs

When using applications, people generally enjoy personalizing their experience to some degree. Allowing your applications users to choose from a set of pre-defined custom themes is one way to accomplish this. Achieving this functionality is actually quite easy through the use of RadControls for WinForms. Today I am going to show you how. Please note that I am using the Q1 2010 beta of the RadControls for WinForms in this blog entry.

Telerik Themes

The default installation of RadControls for WinForms includes several pre-defined themes that you can use in your application. To begin using them, you simply need to drag-and-drop them from the toolbox into the form designer. Once doing so, you can apply one of these themes to your overall application using the ThemeResolutionService.

ThemeResolutionService.ApplicationThemeName = "Breeze";

Custom Themes

Designing your own custom themes for use with RadControls for WinForms is also achievable and is actually quite simple. Visual Style Builder is a custom tool we've included with RadControls for WinForms specifically for use in designing your own custom themes. Once you've designed your custom themes, you can load them into your application through the use of the RadThemeManager. Once doing so, you can again apply them to your application using the ThemeResolutionService.

ThemeResolutionService.ApplicationThemeName = "MyCustomTheme";

Dynamically Changing Themes at RunTime

Now that you've learned about loading Telerik themes and custom themes into your application, lets take a look at giving your users the ability to change the selected theme on-the-fly. You first need to drag-and-drop all of the Telerik themes into the form designer for use in your application. After doing this, you can easy populate a menu w/menu items unique to each theme.

private void PopulateTelerikThemes()
{
CreateThemeMenuItem(mnuTelerikThemes, "Default", "Default");
CreateThemeMenuItem(mnuTelerikThemes, "Aqua", "Aqua");
CreateThemeMenuItem(mnuTelerikThemes, "Breeze", "Breeze");
CreateThemeMenuItem(mnuTelerikThemes, "Desert", "Desert");
CreateThemeMenuItem(mnuTelerikThemes, "Office2007Black", "Office2007Black");
CreateThemeMenuItem(mnuTelerikThemes, "Office2007Silver", "Office2007Silver");
CreateThemeMenuItem(mnuTelerikThemes, "Telerik", "Telerik");
CreateThemeMenuItem(mnuTelerikThemes, "Vista", "Vista");
}
private void CreateThemeMenuItem(RadMenuItem parentMenu, string menuName, string themeName)
{
RadMenuItem newMenuItem = new RadMenuItem(menuName, themeName);
newMenuItem.Click += new EventHandler(themeMenuItem_Click);
parentMenu.Items.Add(newMenuItem);
}

For your custom themes, you will need to place RadThemeManager on to your form. Clicking the smart tag of the RadThemeManager will allow you to load custom themes for use in your application. Once you've included these themes in your application, creating menu items specific to them is quite simple.

private void PopulateCustomThemes()
{
    foreach (string themeName in radThemeManager1.LoadedThemeNames.Distinct<string>())
        CreateThemeMenuItem(mnuCustomThemes, themeName, themeName);
}

Now that you have menu items being generated for each of your themes, updating the theme based on the users selection is as simple as subscribing the menu items to the following click event handler.

void themeMenuItem_Click(object sender, EventArgs e)
{
    RadMenuItem menuItem = sender as RadMenuItem;
    ThemeResolutionService.ApplicationThemeName = menuItem.Tag as string;
}

 

Click here to download the source code used in this post...


Comments

Comments are disabled in preview mode.