schmierlappen
Goto Top

Powershell - Auswahl aus Listbox soll Aktion ausführen

Hi@all,

ich versuche folgendes mit Powershell zu lösen.
Nachdem eine Auswahl in einer Listbox ausgewählt und mit OK bestätigt wurde, soll eine Aktion ausgeführt werden. Hinter jeder Auswahl in der Listbox steht eine andere Aktion.
Das Beispiel unten (welches so nicht funktioniert) soll wie folgt funktionieren:
Aktion 1 wird in der Listbox ausgewählt und es wird der Ordner "Aktion 1" unter C:\Windows angelegt. Bei Auswahl von Aktion 3 wird der Ordner "Aktion 3" unter C:\Windows angelegt, usw...

Und daran scheitere ich im Moment... ich hoffe nach Euren kurzen Lachflash könnt Ihr mir helfen.

Vielen Dank im voraus.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

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

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$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(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Abbruch"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Bitte wählen Sie:"
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)

[void] $listBox.Items.Add("Aktion 1")
[void] $listBox.Items.Add("Aktion 2")
[void] $listBox.Items.Add("Aktion 3")
[void] $listBox.Items.Add("Aktion 4")
[void] $listBox.Items.Add("Aktion 5")

$listBox.Height = 70
$form.Controls.Add($listBox)
$form.Topmost = $True

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{


if ($listBox.Items.Add -eq "Aktion 1")
{
New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 1"
}

if ($listBo.Items.Addx -eq "Aktion 2")
{
New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 2"
}

if ($listBo.Items.Addx -eq "Aktion 3")
{
New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 3"
}

if ($listBo.Items.Addx -eq "Aktion 4")
{
New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 4"
}

if ($listBo.Items.Addx -eq "Aktion 5")
{
New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 5"
}
}

Content-Key: 320174

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

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

Mitglied: 131381
Solution 131381 Nov 06, 2016 updated at 18:43:44 (UTC)
Goto Top
Beispiel zum abgucken ...
function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$btnRun = New-Object System.Windows.Forms.Button
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

$handler_btnRun_Click= 
{
    switch($comboBox1.SelectedItem){
        "Aktion 1" {  
            [System.Windows.Forms.MessageBox]::Show("Aktion 1")  
        }
        "Aktion 2" {  
            [System.Windows.Forms.MessageBox]::Show("Aktion 2")  
        }
        "Aktion 3" {  
            [System.Windows.Forms.MessageBox]::Show("Aktion 3")  
        }
    }
    $form1.Close()
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 47
$System_Drawing_Size.Width = 235
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "Demo Listbox"  

$btnRun.Anchor = 9

$btnRun.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 148
$System_Drawing_Point.Y = 12
$btnRun.Location = $System_Drawing_Point
$btnRun.Name = "btnRun"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnRun.Size = $System_Drawing_Size
$btnRun.TabIndex = 1
$btnRun.Text = "Run"  
$btnRun.UseVisualStyleBackColor = $True
$btnRun.add_Click($handler_btnRun_Click)

$form1.Controls.Add($btnRun)

$comboBox1.Anchor = 13
$comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox1.FormattingEnabled = $True
$comboBox1.Items.AddRange(@("Aktion 1","Aktion 2","Aktion 3"))  
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$comboBox1.Location = $System_Drawing_Point
$comboBox1.Name = "comboBox1"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 130
$comboBox1.Size = $System_Drawing_Size
$comboBox1.TabIndex = 0

$form1.Controls.Add($comboBox1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm
Gruß
Member: TlBERlUS
TlBERlUS Nov 07, 2016 at 07:10:00 (UTC)
Goto Top
Hi,

bei Quellcode bitte immer mit den Code-Tags arbeiten.

Welcher Teil deines Codes arbeitet denn nicht, wie er soll?

Grüße,

Tiberius
Member: Marabunta
Solution Marabunta Nov 07, 2016 updated at 07:40:11 (UTC)
Goto Top
$listBox.SelectedItem löst das Problem

Add-Type -AssemblyName System.Windows.Forms
 Add-Type -AssemblyName System.Drawing

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

 $OKButton = New-Object System.Windows.Forms.Button
 $OKButton.Location = New-Object System.Drawing.Point(75,120)
 $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(150,120)
 $CancelButton.Size = New-Object System.Drawing.Size(75,23)
 $CancelButton.Text = "Abbruch"  
 $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
 $form.CancelButton = $CancelButton
 $form.Controls.Add($CancelButton)

 $label = New-Object System.Windows.Forms.Label
 $label.Location = New-Object System.Drawing.Point(10,20) 
 $label.Size = New-Object System.Drawing.Size(280,20) 
 $label.Text = "Bitte wählen Sie:"  
 $form.Controls.Add($label) 

 $listBox = New-Object System.Windows.Forms.Listbox 
 $listBox.Location = New-Object System.Drawing.Point(10,40) 
 $listBox.Size = New-Object System.Drawing.Size(260,20) 

 [void] $listBox.Items.Add("Aktion 1")  
 [void] $listBox.Items.Add("Aktion 2")  
 [void] $listBox.Items.Add("Aktion 3")  
 [void] $listBox.Items.Add("Aktion 4")  
 [void] $listBox.Items.Add("Aktion 5")  

 $listBox.Height = 70
 $form.Controls.Add($listBox) 
 $form.Topmost = $True

 $result = $form.ShowDialog()

 if ($result -eq [System.Windows.Forms.DialogResult]::OK)
 {


 if ($listBox.SelectedItem -eq "Aktion 1")  
 {
 New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 1"  
 }

 if ($listBox.SelectedItem -eq "Aktion 2")  
 {
 New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 2"  
 }

 if ($listBox.SelectedItem -eq "Aktion 3")  
 {
 New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 3"  
 }

 if ($listBox.SelectedItem -eq "Aktion 4")  
 {
 New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 4"  
 }

 if ($listBox.SelectedItem -eq "Aktion 5")  
 {
 New-Item -ItemType Directory -Path "C:\Windows" -Name "Aktion 5"  
 }
 }
Mitglied: 131381
131381 Nov 07, 2016 at 07:46:59 (UTC)
Goto Top
Ein switch hätte auch vollkommen ausgereicht wie man oben sieht face-smile