Managing Fonts for all controls by code

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
zztemp
Posts: 42
Last visit: Mon Oct 02, 2023 10:51 pm
Answers: 1
Has voted: 2 times

Managing Fonts for all controls by code

Post by zztemp »

Hi,

I know that by using the GUI in PS Studio, you can set the font for each UI-object (control) individually.
But, i'd like to set it for all the controls by code, i've come across examples in C# but not in Powershell.
So i'd like to say, "on form load, set the font for all controls to Fira Code", if it doesn't exist on the machine "set it to verdana 12pt".

Anyone has an example on this?

Much obliged

Z.
by jvierra » Mon Mar 08, 2021 3:25 am
I think that learning PowerShell first would help. You are trying to use some kind of code other than PowerShell for an example.


$font = New-Object System.Drawing.Font('Lucida Console', 12)
$form1.Controls | ForEach-Object{$_.Font = $font}


If there are grouping controls then you will need to recurs3e this.
Go to full post
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Managing Fonts for all controls by code

Post by jvierra »

Enumerate the control collection on the form and set the font property of each control in the collection in a loop.
User avatar
zztemp
Posts: 42
Last visit: Mon Oct 02, 2023 10:51 pm
Answers: 1
Has voted: 2 times

Re: Managing Fonts for all controls by code

Post by zztemp »

Hi, thanks for your feedback. With that i've come to the following piece of code:
  1.    
  2. foreach ($control in $form1.Controls)
  3.     {
  4.         $properties = [System.ComponentModel.TypeDescriptor]::GetProperties($control)
  5.         foreach ($property in $properties.GetEnumerator())
  6.         {
  7.             $property.Font = New-Object System.Drawing.Font("Lucida Console", 12, [System.Drawing.FontStyle]::Regular)
  8.         }
  9.     }
This doesn't work. It says it can't find the property "Font".
But when i do a write-host on `$Property.Name` is does show that property to exist.
I'm close but not there yet. Any more ideas?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Managing Fonts for all controls by code

Post by jvierra »

I think that learning PowerShell first would help. You are trying to use some kind of code other than PowerShell for an example.


$font = New-Object System.Drawing.Font('Lucida Console', 12)
$form1.Controls | ForEach-Object{$_.Font = $font}


If there are grouping controls then you will need to recurs3e this.
User avatar
zztemp
Posts: 42
Last visit: Mon Oct 02, 2023 10:51 pm
Answers: 1
Has voted: 2 times

Re: Managing Fonts for all controls by code

Post by zztemp »

Hi,

My PowerShell is decent enough but i got lost in all the examples in C# on the net.
I over complicated things. Thank you very much for your help jvierra.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Managing Fonts for all controls by code

Post by jvierra »

The problem with most coming to PowerShell and all scripting and programming is lack of correct fundamental knowledge. Learning by example is dangerous without correct fundamentals. WHat you have done and discovered is just that. It is an example of how lack of fundamentals is misleading and causes failure.

As you proceed this lack will become an ever bigger problem because you will copy and learn incorrect things which will lead yoiu into corners and cause you to write code that is unnecessary and copy code that is faulty.

An old joke among programmers is, "if it works once, ship it". This is a tease towards inexperienced coders who have not yet learned all of the fundamentals and a satire on companies that rush products to production without sufficient testing and often without correct design. Just because an example works for someone somewhere does not make it correct so be careful. Take time to nail the fundamentals.

A good place to gain fundamentals of scripting with PowerShell is the following free book. It is simple, easy and written by and for administrators and technicians who are not formally trained in programming. It quickly presents the fundamentals and gives a good overview targeted at how to approach, use and learn PowerShell.

There are many books but this one is free and well accepted. Other books are specially targeted in other ways and mostly assume the reader has some fundamentals like programming skills or deep technical knowledge of networking or operating systems. They are excellent books but generally are not as useful for those just starting or those that need to return to the fundamentals.

https://www.sapien.com/books_training/S ... d-Training

Videos and blogs are useful but only if you have nailed the basics. Without the basics videos and blogs can be misleading.

Good luck.
https://www.sapien.com/books_training/W ... werShell-4

After this book there are many useful videos that will help acquiring PowerShell skills with WinForms and other subsystems.

However you proceed do not skip the fundamentals.
This topic is 3 years and 3 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked