Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dont know how to add bold to a label in winforms powershell

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)
like image 686
IIIdefconIII Avatar asked Jun 18 '17 22:06

IIIdefconIII


4 Answers

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:

  1. The name of a font
  2. The size of the text (you can omit this, but it will default to 1)
  3. The font style, in this case bold.

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.

like image 76
Windos Avatar answered Oct 03 '22 09:10

Windos


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

like image 33
ToTi Avatar answered Oct 03 '22 09:10

ToTi


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)
like image 37
TheNerdAlly Avatar answered Oct 03 '22 09:10

TheNerdAlly


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.

like image 25
JazzyJ Avatar answered Oct 03 '22 10:10

JazzyJ