Powershell Listbox-Eintrag in Textbox ausgeben, wie?
Guten Abend,
aktuell lerne ich die Powershell kennen. Ich wollte das Script hier etwas modifizieren.
Konkret geht es darum, bei einem Klick auf Details, den entsprechenden Eintrag in einer Textbox auszugeben. Dies bekomme ich leider nicht hin. Hat jemand den einen oder anderen Tipp für mich?
Hier mein aktueller Code:
Vielen Dank!
aktuell lerne ich die Powershell kennen. Ich wollte das Script hier etwas modifizieren.
Konkret geht es darum, bei einem Klick auf Details, den entsprechenden Eintrag in einer Textbox auszugeben. Dies bekomme ich leider nicht hin. Hat jemand den einen oder anderen Tipp für mich?
Hier mein aktueller Code:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'ListboxTest'
$form.Size = New-Object System.Drawing.Size(500,500)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,180)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(100,180)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Abbrechen'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$DetailButton = New-Object System.Windows.Forms.Button
$DetailButton.Location = New-Object System.Drawing.Point(180,180)
$DetailButton.Size = New-Object System.Drawing.Size(75,23)
$DetailButton.Text = 'Details'
$DetailButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes
$form.AcceptButton = $DetailButton
$form.Controls.Add($DetailButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(450,50)
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,70)
$listBox.Size = New-Object System.Drawing.Size(450,450)
$listBox.Height = 100
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,220)
$outputBox.Size = New-Object System.Drawing.Size(450,200)
$outputBox.MultiLine = $True #
$outputBox.ScrollBars = "Vertical"
$outputBox.Text = ""
$Form.Controls.Add($outputBox)
[void] $listBox.Items.Add('atl-dc-002')
[void] $listBox.Items.Add('atl-dc-003')
[void] $listBox.Items.Add('atl-dc-004')
[void] $listBox.Items.Add('atl-dc-005')
[void] $listBox.Items.Add('atl-dc-006')
[void] $listBox.Items.Add('atl-dc-007')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}
$DetailButton.Add_Click({
$outputBox.Text = $listBox.SelectedItem.ToString()
})
Vielen Dank!
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 460540
Url: https://administrator.de/forum/powershell-listbox-eintrag-in-textbox-ausgeben-wie-460540.html
Ausgedruckt am: 10.04.2025 um 17:04 Uhr
5 Kommentare
Neuester Kommentar

Dein Click-Event steht viel zu weit unten. Denn die Methode Showdialog() in Zeile 60 ist blockierend, somit kann dein Event erst gar nicht greifen wenn die Form schon angezeigt wird, also platziere die Zeilen 68-70 irgendwo vor Zeile 60.

DetailButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes
$form.AcceptButton = $DetailButton
Ist ja auch kein Wunder wenn du den Button ebenfalls mit DialogResult und als AcceptButton anlegst, denn diese Buttons beenden immer den Dialog! ... Copy n Paste ohne zu wissen was die Sachen bedeuten bringt dich nicht wirklich weiter ...$form.AcceptButton = $DetailButton
https://docs.microsoft.com/de-de/dotnet/framework/winforms/controls/inde ...
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'ListboxTest'
$form.Size = New-Object System.Drawing.Size(500,500)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,180)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(100,180)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Abbrechen'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$DetailButton = New-Object System.Windows.Forms.Button
$DetailButton.Location = New-Object System.Drawing.Point(180,180)
$DetailButton.Size = New-Object System.Drawing.Size(75,23)
$DetailButton.Text = 'Details'
$DetailButton.Add_Click({
$script:outputBox.Text = $listBox.SelectedItem.ToString()
})
$form.Controls.Add($DetailButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(450,50)
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,70)
$listBox.Size = New-Object System.Drawing.Size(450,450)
$listBox.Height = 100
$script:outputBox = New-Object System.Windows.Forms.TextBox
$script:outputBox.Location = New-Object System.Drawing.Size(10,220)
$script:outputBox.Size = New-Object System.Drawing.Size(450,200)
$script:outputBox.MultiLine = $True #
$script:outputBox.ScrollBars = "Vertical"
$script:outputBox.Text = ""
$Form.Controls.Add($script:outputBox)
[void] $listBox.Items.Add('atl-dc-002')
[void] $listBox.Items.Add('atl-dc-003')
[void] $listBox.Items.Add('atl-dc-004')
[void] $listBox.Items.Add('atl-dc-005')
[void] $listBox.Items.Add('atl-dc-006')
[void] $listBox.Items.Add('atl-dc-007')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}