Configuration file for C # forms Winforms

-1

Is there any way to create a file where I can set eg a color with the name 'Background' and add the value '#fff', then use it in all my forms, similar to a CSS style, so in case I need to change this color I do not need to change in all the forms one by one? I've seen something about Singleton, but I do not know if it would be ideal for what I need, which is just reporting a color or text.

    
asked by anonymous 02.07.2018 / 04:50

2 answers

1

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

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

Returning to the WinForms inheritance, it's very simple (Just do not want to solve everything in a single form):

You create a Form with the characteristics that will be applied to all forms of the project:

(NothingchangedintheFormcode)

Now,inthenextForms,youjusthavetochangetheinheritance:

namespaceWindowsFormsApplication{publicpartialclassFormFilho:FormBase{publicFormFilho(){InitializeComponent();}}}

Ready:

NoticethatthepropertiesareloadedwiththedefaultvalueoftheParentForm(FormBase).

  

Comments:

YoucanstillchangethesepropertiesinthechildFormandtheywillapplyonlytoit.

YoucanhaveathirdForm,whichinheritstheFormField,andsoon.

Cautionwhenyouwanttoresolveeverythinginasingleform,addingacontrolintheparentformsforexample,itwillbeavailableinallthechildformsandyouwillnotbeabletoremoveit.RemovingitfromFormparentwillcauseproblemsforchildforms.

ToaccessthetopFormscontrolsintheForms,youonlyhavetochangetheModifierspropertyto the desired value .

    
02.07.2018 / 13:13
4

Maybe, there are two ways to think this. for what he is saying, yes, he has.

It is called object orientation. Something that people say they do, but they are totally unaware of what it is, as it applies.

The unique mechanism of object orientation is inheritance, something I've hardly ever seen anyone do in Windows Forms and it's 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 ones receive these characteristics. What you probably already know is that this control can and probably must inherit from an existing control. And it may be that this new control can be abstract, just to organize. You can let the descendant classes modify the value or not, which may be slightly different than what you want.

If you want the change to occur dynamically, and CSS does not work that way, where it can change on the fly and be automatically transferred to the descendants, then you need a mechanism that does this, probably with events where descendants sign events from the ascendant, perhaps even automatically.

On the other hand it may be speaking not of forms but of controls that are nested inside one another. This does not have anything ready and usually makes little sense in WinForms and probably in other ways except something very restricted and specific. In this case controls that are children of others, that is, are bound within the existing control, such as putting a label or a textbox within a form . The properties of a form are often different. Even those that are equal have different motivations, it does not make much sense to want the form's background color to be automatically replicated in the textbox . What you can do is to set transparent color for example in a label and this means that the background color will be the same as the form because it is not to have a color, it is not white, or black, it is color, this is done.

I can not imagine where Singleton enters this, alias seems to me to be the opposite of what he needs. Unless the question is about something completely different.

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

    
02.07.2018 / 06:04