PowerShell .NET 文本框中的粗体文本
PowerShell .NET Bold text in a textbox
有没有一种简单的方法可以让像 'Version xx' 这样的标题以粗体显示?开始为标题使用标签有点烦人,因为文本会随着时间的推移而增长,并且每次都必须 re-positioned。
代码:
$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)
感谢您的帮助。
感谢以下 Micky 的完整解决方案:
$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)
这是一种使用 RichText 的方法:
$P0Label2 = New-Object System.Windows.Forms.RichTextBox
让我们找到使用过的字体并使用粗体制作新版本:
$oldFont = $P0Label2.Font
$font = New-Object Drawing.Font($oldFont.FontFamily, $oldFont.Size, [Drawing.FontStyle]::Bold)
现在假设我们要在您的文本中突出显示 Version V1.0
。我们首先找到它的索引作为 $P0Label2.Text
的一部分,然后我们使用 SelectionStart 和 SelectionLength 来 select 它,我们将字体更改为粗体版本,我们 deselect selection,将文本保留为粗体。
$string = "Version 1.0"
$P0Label2.SelectionStart = $P0Label2.Text.IndexOf($string)
$P0Label2.SelectionLength = $string.length
$P0Label2.SelectionFont = $font
$P0Label2.DeselectAll()
所以它不是基于标签,但它可以作为解决问题的开始。
有没有一种简单的方法可以让像 'Version xx' 这样的标题以粗体显示?开始为标题使用标签有点烦人,因为文本会随着时间的推移而增长,并且每次都必须 re-positioned。
代码:
$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)
感谢您的帮助。
感谢以下 Micky 的完整解决方案:
$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)
这是一种使用 RichText 的方法:
$P0Label2 = New-Object System.Windows.Forms.RichTextBox
让我们找到使用过的字体并使用粗体制作新版本:
$oldFont = $P0Label2.Font
$font = New-Object Drawing.Font($oldFont.FontFamily, $oldFont.Size, [Drawing.FontStyle]::Bold)
现在假设我们要在您的文本中突出显示 Version V1.0
。我们首先找到它的索引作为 $P0Label2.Text
的一部分,然后我们使用 SelectionStart 和 SelectionLength 来 select 它,我们将字体更改为粗体版本,我们 deselect selection,将文本保留为粗体。
$string = "Version 1.0"
$P0Label2.SelectionStart = $P0Label2.Text.IndexOf($string)
$P0Label2.SelectionLength = $string.length
$P0Label2.SelectionFont = $font
$P0Label2.DeselectAll()
所以它不是基于标签,但它可以作为解决问题的开始。