it-frosch
Goto Top

Text eines Labels unterschiedlich formatieren

Hallo Kollegen,

ich erstelle gerade eine kleine GUI zum Lösen verschiedener Aufgaben.
Zu jeder Aufgabe gibt es eine Überschrift und darunter eine kurze Beschreibung dazu.
Diese Beschreibung realisiere ich aktuell so:

# Label_Task2_D - Aufgabe Nr. 1
$label_Task2D = New-Object System.Windows.Forms.Label
$label_Task2D.Location = New-Object System.Drawing.Point(10, 40)
$label_Task2D.Size = New-Object System.Drawing.Size(600, 90)
$label_Task2D.Text = "Mit dieser Aufgabe kann du viele tolle Dinge tun.   
- ! Denke nicht an den rosa Elefanten ! -
Wünsche dir etwas und drücke anschließend auf den Schalter Änderungen durchführen."  

Nun möchte ich Teile des Textes hervorheben (farbig oder fett).
Geht das überhaupt?
Und falls nicht, wie setzt man so etwas sinnvoll um?


Grüße vom it-frosch

Content-Key: 8089075798

Url: https://administrator.de/contentid/8089075798

Printed on: April 27, 2024 at 05:04 o'clock

Mitglied: 7907292512
Solution 7907292512 Aug 09, 2023 updated at 14:27:30 (UTC)
Goto Top
Entweder separate Labels für unterschiedliche Formatierungen aneinander gehangen, oder ne RichTextbox
$rtfbox = New-Object System.Windows.Forms.RichTextBox -P @{
    Location = '10,10'  
    Size = '180,20'  
    Rtf = "{\rtf1 Ein {\b fetter} Text}"  
    ReadOnly = $true
}

screenshot

Mit WPF hättest du diesbezüglich mehr Möglichkeiten als mit Windows Forms.

Add-Type -AssemblyName Presentationframework
[string]$xaml = @"  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window" Title="Mein Fenster" WindowStartupLocation = "CenterScreen" ResizeMode="NoResize" SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray">  
    <Grid Height="210" Name="Grid1" Width="419">  
        <Label Name="lbl1"/>  
    </Grid>
</Window>

"@  
$window=[Windows.Markup.XamlReader]::Parse($xaml)
$label = $window.FindName('lbl1')  
$textblock = New-Object System.Windows.Controls.TextBlock
$txt1 = New-Object System.Windows.Documents.Run -P @{
    Text = "Red "  
    Foreground = [System.Windows.Media.Brushes]::Red
    FontSize = 20
}
$txt2 = New-Object System.Windows.Documents.Run -P @{
    Text = "Green "  
    Foreground = [System.Windows.Media.Brushes]::Green
    FontWeight = 'Bold'  
    FontSize = 30
}
$txt3 = New-Object System.Windows.Documents.Run -P @{
    Text = "Blue"  
    Foreground = [System.Windows.Media.Brushes]::Blue
    FontFamily = 'Arial Black'  
    FontSize = 25
} 
$textblock.Inlines.Add($txt1)
$textblock.Inlines.Add($txt2)
$textblock.Inlines.Add($txt3)
$label.Content = $textblock
$async = $window.Dispatcher.InvokeAsync({$window.ShowDialog() | Out-Null})
$async.Wait() | Out-Null

screenshot

Gruß siddius
Member: it-frosch
it-frosch Aug 09, 2023 at 14:38:41 (UTC)
Goto Top
@7907292512

perfekt, das hilft echt weiter.

grüße vom it-frosch