Label: howto set 1 letter in other color andfo

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 6 years and 6 months 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
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Label: howto set 1 letter in other color andfo

Post by stevens »

Hi,

Hope this is a simple question: how do I set a single font in a label to a different color and font?

I would like to set a label with text "Add users/groups/computer$" with the $ in red and in fat.
J.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Label: howto set 1 letter in other color andfo

Post by brittneyr »

That isn't possible, a label can only have one color. If you wish to have two colors then you will need two labels.
viewtopic.php?t=10424
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Label: howto set 1 letter in other color andfo

Post by jvierra »

You can use the "Paint" event to create almost any graphic form you want including a multi-color label text.
Here is a quick lesson on graphics and how to "paint" graphics using PowerShell:

http://www.techotopia.com/index.php/Dra ... and_GDI%2B

With a control just grab the $_.Graphics object and use the methods to write your text in any color or image you like.

Code: Select all

$label1_Paint=[System.Windows.Forms.PaintEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
	$this.Text = ''
	$font = [System.Drawing.Font]'Microsoft Sans Serif, 13.8pt, style=Bold, Italic'
	$brush = [System.Drawing.Brushes]::Red
	$_.Graphics.DrawString('$',$font ,$brush,2,2)
	$brush = [System.Drawing.Brushes]::DarkGreen
	$_.Graphics.DrawString('Whoopie', $font, $brush, 21, 2)
}
label_me.png
label_me.png (3.41 KiB) Viewed 2888 times
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Label: howto set 1 letter in other color andfo

Post by stevens »

Great input, thanks!
This topic is 6 years and 6 months 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