2

Is there a simple way to have the titles like 'Version xx' displayed in bold? It's a bit annoying to start using labels for the titles, as the text will grow in time and has to be re-positioned every time then.

Code:

$P0Label2 = New-Object System.Windows.Forms.TextBox
$P0Label2.Location = New-Object System.Drawing.Point(8,28)
$P0Label2.Size = New-Object System.Drawing.Size(516,340)
$P0Label2.ReadOnly = $True
$P0Label2.WordWrap = $True
$P0Label2.ScrollBars = 'Vertical'
$P0Label2.Multiline = $True
$P0Label2.BackColor = 'LightSteelBlue'
$P0Label2.Text = 
    "Version 2.0:",
    "- 2015/01/05 Stuff",
    "- 2015/01/09 Stuff",
    "Version 1.0:",
    "- 2014/04/25 Stuff" | foreach {"$_`r`n"}
$P0.Controls.Add($P0Label2) 

Thank you for your help.

Full solution thanks to Micky below:

$P0Label2 = New-Object System.Windows.Forms.RichTextBox
$P0Label2.Location = New-Object System.Drawing.Point(8,28)
$P0Label2.Size = New-Object System.Drawing.Size(516,340)
$P0Label2.ReadOnly = $True
$P0Label2.WordWrap = $True
$P0Label2.ScrollBars = 'Vertical'
$P0Label2.Multiline = $True
$P0Label2.BackColor = 'LightSteelBlue'
$P0Label2.Text = 
    "Version 2.0:",
    "- 2015/01/05 Stuff",
    "- 2015/01/09 Stuff",
    "Version 1.0:",
    "- 2014/04/25 Stuff" | foreach {"$_`r`n"}

"Version 2.0:",
"Version 1.0:" | foreach {
    $oldFont =  $P0Label2.Font
    $font = New-Object Drawing.Font($oldFont.FontFamily, $oldFont.Size, [Drawing.FontStyle]::Bold)
    $string = $_
    $P0Label2.SelectionStart = $P0Label2.Text.IndexOf($string)
    $P0Label2.SelectionLength = $string.length
    $P0Label2.SelectionFont = $font
    $P0Label2.DeselectAll()
}
$P0.Controls.Add($P0Label2) 
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • possible duplicate of [How do I set a textbox's text to bold at run time?](http://stackoverflow.com/questions/3089033/how-do-i-set-a-textboxs-text-to-bold-at-run-time) – Vikas Gupta Jan 09 '15 at 09:29
  • Not really, I was more looking for something that allows for more flexibility like `b\b`. But this doesn't work.. – DarkLite1 Jan 09 '15 at 09:43

1 Answers1

2

Here is a way of doing it using RichText:

$P0Label2 = New-Object System.Windows.Forms.RichTextBox

Let's find the used font and make a new version using bold:

$oldFont =  $P0Label2.Font
$font = New-Object Drawing.Font($oldFont.FontFamily, $oldFont.Size, [Drawing.FontStyle]::Bold)  

Let's now say we want to hightlight Version V1.0 within your text. We first find its index as part of $P0Label2.Text, then we use SelectionStart and SelectionLength to select it, we change the font to a bold version, and we deselect the selection, leaving the text in bold.

$string = "Version 1.0"
$P0Label2.SelectionStart = $P0Label2.Text.IndexOf($string)
$P0Label2.SelectionLength = $string.length
$P0Label2.SelectionFont = $font
$P0Label2.DeselectAll()

So it's not based on tags, but it can be a start to work something out.

Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31