How can I easily maintain consistent user interface options in a C # Winform application?

I have many different UserControls and I would like to maintain consistent user interface settings (mainly colors and fonts). My first attempt was as follows:

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

which is called in each control as follows:

 settings.SetupUserControl(ref this); 

Since this is read-only, it cannot be passed with the ref argument, so this does not work. What are the other options for maintaining a consistent user interface without manually changing properties for each element?

+4
source share
4 answers

Do the same. Do not pass it by reference. UserControl is already a reference object, so there is no need to pass it to your method using the ref keyword.

You can also consider a recursive method that will find all UserControl elements in the form and pass them to your method.

+3
source

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

 CustomForm : Form // Your custom form. Form1 : CustomForm // Inherit from it. 

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

Whenever you want to make changes to your basic styles or any settings, just change the settings of your custom controls - your new forms / controls will be automatically updated!

+5
source

What about a base class that provides such settings?

+1
source

Two answers:

  • You do not need ref , controls are reference types. Just let it go.
  • Create a basic UserControl and create your own controls for this base. You can still do this, just edit the class definitions of the controls. For new controls, you can follow the wizard.

Tip: customize the style in baseControl. Then make sure that the derived controls are not overridden; the best way to do this is to scan the * .Designer.cs files and delete all the settings that you know should come from the database.

+1
source

Source: https://habr.com/ru/post/1304092/


All Articles