vornamenachname
Goto Top

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:

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!

Content-Key: 460540

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

Printed on: April 24, 2024 at 09:04 o'clock

Mitglied: 139920
139920 Jun 08, 2019 updated at 18:16:04 (UTC)
Goto Top
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.
Member: VornameNachname
VornameNachname Jun 08, 2019 at 18:44:30 (UTC)
Goto Top
Habe ich getan, leider gleiches Ergebnis. Die Textbox bleibt leer und das Script schließt sich.
Mitglied: 139920
Solution 139920 Jun 08, 2019 updated at 20:00:32 (UTC)
Goto Top
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 ...
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
}
Member: LeeX01
LeeX01 Jun 08, 2019 at 20:59:28 (UTC)
Goto Top
kleiner Tipp noch:
schau dir mal Poshgui.com das dürfte dir helfen dein Vorhaben schnell umzusetzen und die Lernkurve ist auch grad am Anfang hoch.

Grüße
Member: VornameNachname
VornameNachname Jun 09, 2019 at 16:50:03 (UTC)
Goto Top
Funktioniert! ;)
Poshgui werde ich mir mal anschauen.
Danke euch!