i tried several things from different post, but i just cant seem to make it bold
Does anybody knows how to accomplish this?
$LabelComputer = New-Object System.Windows.Forms.Label
$LabelComputer.Text = "Computer Settings"
$LabelComputer.AutoSize = $True
$LabelComputer.Top="5"
$LabelComputer.Left="10"
$LabelComputer.Anchor="Left,Top"
$form1.Controls.Add($LabelComputer)
You're going to need to create a Font
object and give that to the Font
property on your label control.
Unfortunately with these objects you need to give it a few things in the constructor, so you can't just create a blank object and fill it with details like you can with the label.
To that end you can do this before adding the control to your form:
$LabelComputer.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
You'll notice in inside that new(...)
three things:
You'll need to adjust the Font and Size to fit your needs.
Note, if you're creating many labels using the same font, create your font object and assign it to $LabelFont
then your property on the label can be $LabelComputer.Font = $LabelFont
.
I dont know about powershell version discrepancy, but for sure solution with [System.Drawing.Font]::new
is not working in powershell 2.0 (Idk how its in 5.1 version).
I found another solution how you can set text in label to bold. Like this:
$LabelComputer.Font = New-Object System.Drawing.Font("Arial",8,[System.Drawing.FontStyle]::Bold)
Where instead of Bold you can use following: Regular, Bold, Italic, Underline, Strikeout
Adding to above @ToTi, the following worked for me (verified using vs 5.1)
$LabelComputer.Font = new-object System.Drawing.Font('Ariel',8,[System.Drawing.FontStyle]::Bold)
I was strugging with this today, managed to come up with this whereby it keeps the original font type and size and just amends it to a Bold style.
$mylabel.Font = [system.drawing.font]'$mylabel.Font.Name$mylabel.Font.Size, style=Bold'
Works on Version 5.1 of PowerShell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With