an34mem
Goto Top

Powershell ListBox mit Trennstrich oder Text (Nachfrage)

Ich möchte gern diesen alten Thread nochmals aufgreifen und in die Runde fragen:
Powershell ListBox mit Trennstrich oder Text

Gibt es vielleicht eine Möglichkeit das Beispiel bei einer Combobox anzuwenden, wo die Trennstriche nicht auswählbar sind?
Leider haben meine Versuche nicht den Erfolg gebracht.

Interessant wäre auch, wenn jede 2 Item in einer anderen Farbe angezeigt werden könnte.
Hat jemand vielleicht einen Tipp, wie man so etwas umsetzen kann?

Content-Key: 643026

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

Printed on: April 19, 2024 at 06:04 o'clock

Member: H41mSh1C0R
H41mSh1C0R Jan 21, 2021 at 13:33:43 (UTC)
Goto Top
Was hast du denn schon gebaut?

Von der Herangehensweise würde ich mal sagen, beim Click-Event den zurückgegebenen Inhalt prüfen und bei den Trennstrichen einfach nix machen.

vg
Member: AN34Mem
AN34Mem Jan 21, 2021 at 13:37:47 (UTC)
Goto Top
$x = @()

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")   

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"  
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"  

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") {  
        $script:x = $objcombobox.SelectedItems
        $objForm.Close()
    }
})

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")   
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"  

$OKButton.Add_Click(
   {
       $script:x = $objcombobox.SelectedItems
       $objForm.Close()
   })

$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"  
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please make a selection from the list below:"  
$objForm.Controls.Add($objLabel) 

$objcombobox = New-Object System.Windows.Forms.ComboBox 
$objcombobox.Location = New-Object System.Drawing.Size(10,40) 
$objcombobox.Size = New-Object System.Drawing.Size(260,20) 

$objcombobox.SelectionMode = "MultiExtended"  

$trennstriche = @('----------Test-Eintrag----------','','Test2')  

$objcombobox.Items.AddRange(@(
"Item 1"  
"Item 2"  
"Item 3"  
$trennstriche
"Item 4"  
"Item 5"  
$trennstriche
"Item 6"  
$trennstriche[1]
"Item 7"  
$trennstriche
"Item 8"  
$trennstriche[2]
"Item 9"  
))

$objcombobox.Height = 70

$objcombobox.add_SelectedIndexChanged({
   $objcombobox.SelectedIndices | ?{$objcombobox.GetSelected($_) -and $objcombobox.Items[$_] -in $trennstriche} | %{$objcombobox.SetSelected($_,$false)}
})

$objForm.Controls.Add($objcombobox) 
$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x
Member: H41mSh1C0R
H41mSh1C0R Jan 21, 2021 at 13:42:57 (UTC)
Goto Top
Gut und nun baue die IF Abfrage in das Click Event des OK Buttons ein und alles wird gut.

Wo ist das Problem?
Member: AN34Mem
AN34Mem Jan 21, 2021 at 13:47:01 (UTC)
Goto Top
Mit einer Listbox funktioniert das einwandfrei,
aber leider nicht mit einer Combobox.

Das Mitglied '114757 (Level 4)' hat die Lösung für eine Listbox veröffentlicht, ist aber nicht mehr erreichbar.
Ansonsten hätte ich Ihn gefragt..
Mitglied: 147323
147323 Jan 21, 2021 updated at 14:24:45 (UTC)
Goto Top
Mit Windows Forms geht das nicht. Wohl aber mit WPF, damit hast du wesentlich mehr Möglichkeiten.

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" Title="MainWindow" Height="159" Width="288">  
    <Grid>
        <ComboBox Height="29" HorizontalAlignment="Left" Margin="64,43,0,0" Name="comboBox1" VerticalAlignment="Top" Width="157" ItemsSource="{Binding}" AlternationCount="2">  
            <ComboBox.Resources>
                <Style TargetType="{x:Type ComboBoxItem}">  
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">  
                            <Setter Property="Background" Value="White" />  
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">  
                            <Setter Property="Background" Value="LightGray" />  
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.Resources>
            <ComboBoxItem Content="Item1" />  
            <ComboBoxItem Content="Item2" />  
            <Separator />
            <ComboBoxItem Content="Item3" />  
            <ComboBoxItem Content="Item4" />  
        </ComboBox>
    </Grid>
</Window>

"@  
$window=[Windows.Markup.XamlReader]::Parse($xaml)
$async = $window.Dispatcher.InvokeAsync({$window.ShowDialog() | Out-Null})
$async.Wait() | Out-Null