views:

115

answers:

3

I have a lot of different UserControls and would like to maintain consistent UI settings (mainly colors and fonts). My first try was this:

public class UISettings
{
//...
    public void SetupUserControl(ref UserControl ctrl)
    {
        ctrl.BackColor = this.BackColor;
    }
}

to be called in every control like this:

settings.SetupUserControl(ref this);

As this is read-only it cannot be passed by ref argument so this does not work. What are other options to keep consistent UI without manually changing properties for every item?

+1  A: 

How about a base class which provides such settings?

Scoregraphic
+3  A: 

Do the same thing. Don't pass it by ref. UserControl is a reference object already, so there's no need to pass it into your method using the ref keyword.

You may also want to consider a recursive method that will find all the UserControls on the form and pass it into your method.

David Morton
I did the method recursive and it works just perfect, thanks.
Lukas
+4  A: 

Inheritance! If you have a form or control that will constantly be using the same styles and you want to set that as your base, just create your own user controls that inherit from a form/control. By default all of your forms will inherit from "Form". Instead of inheriting from the default form, create a new user control that inherits from Form, and then have that as your base class.

CustomForm : Form // Your custom form.

Form1 : CustomForm // Inherit from it.

...the same works for components. If you want a button to have the same styles across the board, create a user control and have it inherit from the button control -- then use the custom control.

Whenever you want to make a change to your base styles, or any settings, simply change your custom controls settings -- your new forms/controls will automatically be updated!

George
I used to do this, and it's a good idea (+1), but recently I decided it's not good enough. That problem is that it lacks built-in runtime theming/skin support. The built-in support is only at design time. You could of course build that in to your inherited controls, but that's extra work. If you're gonna have to write extra code to support themes (and if you've got a specific-enough style in mind to justify inheritance, you probably should), you may as well write that code to work with any stock control.
Joel Coehoorn
Well, use the right tool for the right job. No one solution is global. :)
George