Powershell ListBox mit Trennstrich oder Text
Hallo,
ist es möglich in einer ListBox eine Trennung einzubauen sodass z.B. zwischen Item 3 und 4 eine extra Zeile mit Text ist die am besten auch nicht ausgewählt werden kann?
ist es möglich in einer ListBox eine Trennung einzubauen sodass z.B. zwischen Item 3 und 4 eine extra Zeile mit Text ist die am besten auch nicht ausgewählt werden kann?
$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")
{
foreach ($objItem in $objListbox.SelectedItems)
{$x += $objItem}
$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(
{
foreach ($objItem in $objListbox.SelectedItems)
{$x += $objItem}
$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)
$objListbox = New-Object System.Windows.Forms.Listbox
$objListbox.Location = New-Object System.Drawing.Size(10,40)
$objListbox.Size = New-Object System.Drawing.Size(260,20)
$objListbox.SelectionMode = "MultiExtended"
[void] $objListbox.Items.Add("Item 1")
[void] $objListbox.Items.Add("Item 2")
[void] $objListbox.Items.Add("Item 3")
[void] $objListbox.Items.Add("Item 4")
[void] $objListbox.Items.Add("Item 5")
$objListbox.Height = 70
$objForm.Controls.Add($objListbox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 281014
Url: https://administrator.de/contentid/281014
Ausgedruckt am: 08.11.2024 um 21:11 Uhr
9 Kommentare
Neuester Kommentar
$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 = $objListbox.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 = $objListbox.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)
$objListbox = New-Object System.Windows.Forms.Listbox
$objListbox.Location = New-Object System.Drawing.Size(10,40)
$objListbox.Size = New-Object System.Drawing.Size(260,20)
$objListbox.SelectionMode = "MultiExtended"
$trennstrich = '-----'
$objListBox.Items.AddRange(@(
"Item 1"
"Item 2"
"Item 3"
$trennstrich
"Item 4"
"Item 5"
"Item 6"
))
$objListbox.Height = 70
$objListbox.add_SelectedIndexChanged({
if($objListbox.SelectedItems -contains $trennstrich){
$objListbox.SetSelected($objListbox.FindStringExact($trennstrich),$false)
}
})
$objForm.Controls.Add($objListbox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
Kannst du mir noch sagen wie es aussehen müsste wenn ich jetzt $trennstrich2 oder mehr haben wollen würde?
$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 = $objListbox.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 = $objListbox.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)
$objListbox = New-Object System.Windows.Forms.Listbox
$objListbox.Location = New-Object System.Drawing.Size(10,40)
$objListbox.Size = New-Object System.Drawing.Size(260,20)
$objListbox.SelectionMode = "MultiExtended"
$trennstriche = @('----------Test-Eintrag----------','','Test2')
$objListBox.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"
))
$objListbox.Height = 70
$objListbox.add_SelectedIndexChanged({
$objListbox.SelectedIndices | ?{$objListbox.GetSelected($_) -and $objListbox.Items[$_] -in $trennstriche} | %{$objListbox.SetSelected($_,$false)}
})
$objForm.Controls.Add($objListbox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
Ja nee, ein leerer Eintrag geht damit so nicht. Und warum schreibst du zwei mal den selben EIntrag ins Array ?? Das geht erst recht nicht. Das Array darf nur eindeutige nicht leere Einträge haben.
Habs in meinem letzten Post nochmal angepasst damit gehen auch leere Einträge ...