Wie kann ich ein PowerShell-Skript mit dem X-Button beenden?
Ich habe eine MessageBox und möchte das Skript beenden, sobald X gedrückt wird.
Kann ich eine Variable hochzählen, wenn ein anderer Button gedrückt wird? Eine Variable, die verhindert, dass mein Skript beendet wird? Mit onClick oder so?
$version = "xy"
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$START = New-Object system.Windows.Forms.Form
$START.ClientSize = '400,282'
$START.text = $version
$START.TopMost = $false
$Label1a = New-Object system.Windows.Forms.Label
$Label1a.text = "Inhaltt:"
$Label1a.AutoSize = $true
#$Label1a.width = 25
#$Label1a.height = 10
$Label1a.location = New-Object System.Drawing.Point(9, 24)
$Label1a.Font = 'Microsoft Sans Serif,10'
$Button1a = New-Object system.Windows.Forms.Button
$Button1a.text = "OK"
$Button1a.width = 381
$Button1a.height = 30
$Button1a.location = New-Object System.Drawing.Point(9, 243)
$Button1a.Font = 'Microsoft Sans Serif,10'
$Button1a.add_Click({$START.close()}) #MessageBox wird geschlossen
$TextBox1a = New-Object system.Windows.Forms.TextBox
#$TextBox1a.multiline = $false
$TextBox1a.text = "Inhalt"
$TextBox1a.width = 55
$TextBox1a.height = 20
$TextBox1a.location = New-Object System.Drawing.Point(9, 45)
$TextBox1a.Font = 'Microsoft Sans Serif,10'
$TextBox2a = New-Object system.Windows.Forms.TextBox
#$TextBox2.multiline = $false
$TextBox2a.text = "Inhalt"
$TextBox2a.width = 381
$TextBox2a.height = 20
$TextBox2a.location = New-Object System.Drawing.Point(9, 119)
$TextBox2a.Font = 'Microsoft Sans Serif,10,style=Italic'
$Label2a = New-Object system.Windows.Forms.Label
$Label2a.text = "Inhalt:" #$inputpath #Variable muss noch definiert werden
$Label2a.AutoSize = $true
#$Label2a.width = 25
#$Label2a.height = 10
$Label2a.location = New-Object System.Drawing.Point(9, 98)
$Label2a.Font = 'Microsoft Sans Serif,10'
$Label3a = New-Object system.Windows.Forms.Label
$Label3a.text = "Inhalt:" #$outputpath
$Label3a.AutoSize = $true
#$Label3a.width = 25
#$Label3a.height = 10
$Label3a.location = New-Object System.Drawing.Point(9, 172)
$Label3a.Font = 'Microsoft Sans Serif,10'
$TextBox3a = New-Object system.Windows.Forms.TextBox
#$TextBox3a.multiline = $false
$TextBox3a.text = "Inhalt"
$TextBox3a.width = 381
$TextBox3a.height = 20
$TextBox3a.location = New-Object System.Drawing.Point(9, 192)
$TextBox3a.Font = 'Microsoft Sans Serif,10,style=Italic'
$START.controls.AddRange(@($Label1a, $Button1a, $TextBox1a, $TextBox2a, $Label2a, $Label3a, $TextBox3a, $Label1ab))
[void]$START.ShowDialog()
1 Antwort
Wie kann ich ein powershell script mit dem X button beenden?
Indem du auf ihn klickst.
Kann ich eine Variable hochzählen wenn ein anderer Button gedrückt wird, (...)
Statte alle Butons mit einem entsprechenden Event Handlers für das Click-Event aus. Für Minimieren / Maximieren musst du dich auf das Resize-Event konzentrieren. Die Antwort von Robinicks sollte dir dabei etwas weiterhelfen.
In den jeweiligen Handlers kannst du wiederum deine Variable hochzählen.
(...) eine Variable die verhindert, dass mein script beendet wird? (...)
Definiere einen Event-Handler für FormClosing. Über das Event-Objekt, welches der Methode übergeben wird, kannst du die Aktion abbrechen:
$START.add_FormClosing({ $_.cancel = $true })
Wenn du im Handler nun eine Variable einbinden möchtest, erstelle eine andere, deren Scope du erweiterst.
Beispiel:
$someVariable = 1
$START.add_FormClosing({
$Script:somOtherVariable = $someVariable
if ($Script:somOtherVariable -eq 1) {
$_.cancel = $true
}
})