Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily keep consistent UI settings in C# Winform application?

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?

like image 533
Lukas Avatar asked Dec 29 '22 23:12

Lukas


1 Answers

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!

like image 84
George Johnston Avatar answered Jan 12 '23 04:01

George Johnston