I am writing a windows forms application. I have many forms, I want to set all properties at once.
-
_All_ properties? I guess just some, right? Like a "skin", somehow? Do you want hardcoded static values or should they be changeable through code? You could bind properties to a shared model. Which for Forms' properties would likely be some Settings. – Fildor Nov 11 '19 at 13:40
-
Not all properties but all forms share same properties – Eslam Nano Nov 11 '19 at 13:41
-
Then you need a collection of all Forms. I am 90% sure the Application it self has something like that, but when in doubt you could make one yourself. – Christopher Nov 11 '19 at 13:41
-
Have a look into: [DataBinding](https://learn.microsoft.com/en-us/dotnet/framework/winforms/windows-forms-data-binding) , [AppSettings](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings), ... – Fildor Nov 11 '19 at 13:48
-
do you already use inheritance? – Rovann Linhalis Nov 11 '19 at 14:05
-
@RovannLinhalis No i want any way to make all forms share properties values from one place – Eslam Nano Nov 11 '19 at 14:27
-
Make a Form *template* (a class derived from `Form`) and derive your Forms from this *model* (derive each Form that share the same defaults - properties values/layout- from your *base class*). A sample [here](https://stackoverflow.com/a/56533229/7444103). You'll find some more examples searching SO. – Jimi Nov 11 '19 at 14:34
2 Answers
What you need is a collection of all the Forms. While you could make your own, actually the Application already has one. It is what it uses to (among other things) send Form.Close() to every Form when it receives a close request from the OS. Or what it uses to decide "all forms closed, time to end".
As WindowsForms is from the pre-generics era, there is a special collection for this: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.formcollection
Nowadays a List<Form>
will do, but when you get soemthing it will be in that old type. The static Propery to get them from is Application.OpenForms.
Caveates of OpenForms
Note however that this only includes opened forms. Forms on wich Show() or ShowDialog() has been called, and that have not yet been Closed(). Forms that got instantiated but not opened or already were closed? The application does not care about those. The ones not yet created? You would to apply this manually.
IIRC with there was some special cases with Forms that have been opened but since hidden (rather then closed). I operated under the standing advise never to use that feature, as it causes all sorts of issues. So I am not sure what exactly it does in regards to that list.

- 9,634
- 2
- 17
- 31
You can use PropertyBinding
, and the value is save on Settings from application.
I make one example:
In Properties window, select Application Settings, PropertyBinding:
And select whats property you want, if exists the setting select, or create new:
After, you can change the value on Settings window, and all forms with setting, will be changed:
Result:
Values are save in the .config file:
<applicationSettings>
<WindowsFormsPropertiesBind.Properties.Settings>
<setting name="BackColor" serializeAs="String">
<value>LightBlue</value>
</setting>
</WindowsFormsPropertiesBind.Properties.Settings>
</applicationSettings>

- 601
- 8
- 14