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.
Managing Fonts for all controls by code
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.
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.
Managing Fonts for all controls by code
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$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.
Re: Managing Fonts for all controls by code
Hi, thanks for your feedback. With that i've come to the following piece of code:
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?
- foreach ($control in $form1.Controls)
- {
- $properties = [System.ComponentModel.TypeDescriptor]::GetProperties($control)
- foreach ($property in $properties.GetEnumerator())
- {
- $property.Font = New-Object System.Drawing.Font("Lucida Console", 12, [System.Drawing.FontStyle]::Regular)
- }
- }
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?
Re: Managing Fonts for all controls by code
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.
$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.
Re: Managing Fonts for all controls by code
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.
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.
Re: Managing Fonts for all controls by code
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.
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.