Log in to like this post! Application Styling - Style Selector User Control for WinForms Tom Puglisi / Tuesday, February 24, 2009 Quite often, I notice myself having to write similar logic to allow end-users the ability to select one of the cool Application Styling themes that ship with Infragistics NetAdvantage Windows Forms controls. As I was sitting here I thought "Wouldn't it be cool if I just had a user control that I can toss onto a configuration form that allows the end-user to select from a list of styles that exist in a directory?" Well, I created a user control that does exacly that and here is what I did: I created a project named AppStylingLib and this contains a user control named AppStyleUserControl.cs, This user control allows end-users to select from the list of styles that are found in the Styles directory. This user control also loads and persists selected styles to a settings file. The project also contains a settings file named AppStylingLibSettings.settings. This is responsible for persisting end-user selected styles. Here is the source code for the user control: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using Infragistics.Win.AppStyling; namespace AppStylingLib { public partial class AppStyleUserControl : UserControl { public AppStyleUserControl() { InitializeComponent(); } private static string _StylePath = string.Empty; //This is static so that we can set this property before we even load the //user control instance. e.g., we set this property from the Main( ) method. public static string StylePath { get { return _StylePath; } set { _StylePath = value; } } private void AppStyleUserControl_Load(object sender, EventArgs e) { this.LoadUI(); } /// <summary> /// Clicking this button loads and persists the current Application Style /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnApply_Click(object sender, EventArgs e) { if (this.cboStyles.SelectedIndex != -1) { string theStyleFile = this.cboStyles.SelectedItem.DataValue.ToString(); StyleManager.Load(theStyleFile); AppStylingLibSettings.Default.AppStyle = Path.GetFileName(theStyleFile); AppStylingLibSettings.Default.Save(); } } /// <summary> /// This method loads or refreshes the controls on the form. /// This means that the WinComboEditor will scan the directory you specified /// and populate with any Style files (*.isl) /// </summary> public void LoadUI() { this.cboStyles.Items.Clear(); if (Directory.Exists(StylePath)) { string[] theFiles = Directory.GetFiles(StylePath, "*.isl"); if (theFiles.Length > 0) { foreach (string theFile in theFiles) { this.cboStyles.Items.Add(theFile, Path.GetFileNameWithoutExtension(theFile)); } } //Check to see if we have a persisted style string theStyle = AppStylingLibSettings.Default.AppStyle.Trim(); if (this.cboStyles.Items.Count > 0 && theStyle.Length > 0) { //If we do, then we pre-select the style within the Combo, //so that we know what style is currently applied this.cboStyles.Value = Path.Combine(StylePath, theStyle); } } } /// <summary> /// This method allows us to load a style that was persisted. /// </summary> public static void LoadePersistedStyle() { string theStyle = AppStylingLibSettings.Default.AppStyle.Trim(); if (theStyle.Length > 0) { StyleManager.Load(Path.Combine(StylePath, theStyle)); } } } } This User Control is used as follows: Place the user control on any application form that you designate as a "configuration" form. Determine where *.isl files will be stored (you may want to provide a default location as well as allow end-users to specify a custom path). Inform the user control of the *.isl path and then let it load (if any) previously persisted styles. Here is the Main method of a sample application that uses this user control: /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { //Use the static members to set things up: AppStylingLib.AppStyleUserControl.StylePath = Application.StartupPath + @"\Styles"; AppStylingLib.AppStyleUserControl.LoadePersistedStyle(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } This application assumes that you have a subdirectory named Styles that contains several *.isl files. Please feel free to download and check out the source code for this sample: http://download.infragistics.com/users/TomP/AppStylingUserControlTest.zip