Configuration file for C# Winforms

Asked

Viewed 256 times

-1

Is there any way to create a file where I can set for example a color with the name 'Background' and add the value '#fff', then use it in all my Forms, similar to a style of CSS, so that if I need to change this color I don’t need to change in all the Forms one by one? I saw something about Singleton, but I don’t know if it would be ideal for what I need, which is just inform a color or text.

2 answers

4

Maybe there’s two ways to think about it. From what you’re saying, yes, there is.

It’s called object orientation. Something that people say they do, but they totally don’t know what it is, how it applies.

The unique object-oriented mechanism is inheritance, something I’ve virtually never seen anyone do in Windows Forms and where OOP best applies.

You create a control with certain characteristics and whenever you need a similar control that maintains the same characteristics you inherit from this control, so all inherited receive these characteristics. What you probably already know is that this control can, and probably should, inherit from an existing control. And it may be that this new control can be abstract, just to organize. You can let the descending classes change the value or not, which may be a little different than you want.

If you want the change to happen dynamically, and the CSS doesn’t work like that, where you can change on time and automatically transfer to the descendants, then you need a mechanism that does this, probably with events where the descendants sign events from the ascendant, maybe even automatically.

On the other hand it may be speaking not of forms but of controls that are nested within each other. This has nothing ready and usually makes little sense in Winforms and probably in other forms, except something very restricted and specific. In this case the controls that are children of others, ie, are linked within the existing control, something like putting a label or a textbox within a form. The properties of a form are usually different. Even those that are equal have differences motivations, it doesn’t make much sense to want the background color of the form to be automatically replicated in textbox. What you can do is establish transparent color for example in a label and that means that the background color will be the same as the form because it’s not to have a color, it’s not white, or black, it’s colorless, it’s done.

I can’t imagine where Singleton comes into this, by the way, he seems to be the opposite of what he needs. Unless the question is about something else altogether.

The question is very general and the answer was in the same line.

1


As Maniero said, the Windows Forms, You use the inheritance. A Singleton, or create "a file where I can define ... a color with the name 'Background' and add the value '#fff'" may even work, but it will be gambiarra. Just imagine every builder of Form you have to go there and put: this.BackColor = minhaClasseStylesSingleton.Fundo;

Such use would be feasible, but for specific points, for example, define messages, or colors of certain objects.

Back to the Winforms inheritance, it’s pretty simple (Just don’t want to solve everything in a single form):

You create a Form with the features that will be applied to all Foms in the project:

inserir a descrição da imagem aqui

(nothing was changed in the Form code)

Now, in the next Forms, you just change the inheritance:

namespace WindowsFormsApplication
{
    public partial class FormFilho : FormBase
    {
        public FormFilho()
        {
            InitializeComponent();
        }
    }
}

Ready:

inserir a descrição da imagem aqui

Note that properties are loaded with the default Form Parent value (Formbase).

Remarks:

You can still change these properties in the child form and they will only be applied to it.

You can have a third Form, which inherits the Formfilho and so on.

Be careful when you want to solve everything in one Form, add a control in the parent Forms for example, it will be available in all child Forms and it will not be possible to remove it. Remove it from the Form father, will bring problems to the Forms children.

To access the top Forms controls in Child Forms, simply change the property Modifiers for the desired value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.