Benutzer mit den meisten Antworten
Event-Handling on Powershell-GUI (System.Windows.Forms)

Frage
-
Hey guys
I'm Using the System.Windows.Forms Namespace to create a GUI, so I got the following Code.
# Load Assemblies [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") # Init Form $Form = New-Object System.Windows.Forms.Form $Form.width = 300 $Form.height = 150 # Add Checkbox $Checkbox = new-object System.Windows.Forms.Checkbox $Checkbox.Location = new-object System.Drawing.Size(10,10) $Checkbox.size = new-object System.Drawing.Size(20,20) $Form.Controls.Add($Checkbox) # Add Textbox $Textbox = new-object System.Windows.Forms.Textbox $Textbox.Location = new-object System.Drawing.Size(50,10) $Textbox.size = new-object System.Drawing.Size(100,20) $Textbox.Text = "" $Form.Controls.Add($Textbox) # Add Click-Event $Event = Register-ObjectEvent -inputObject $Checkbox -eventName Click -Action {$Textbox.Text = "IT WORKS"} # Create Form $Form.Add_Shown({$Form.Activate()}) $Form.ShowDialog()
As you can see I added a Checkbox and a Textbox to the Form, now I want to handle the Checkbox.Click Event. Unfortunately it doesn't work like that, does anybody have an idea if it's even possible and, if so, where it should be placed at the Script?
Any Help would be appreciated!
Thanks so far.
Antworten
-
To add an event to an Winforms-object you need to use the "add_eventname()"-syntax :
# Add Click-Event $Checkbox.add_CheckStateChanged({$Textbox.Text = "IT WORKS"})
To delete the text again if the box is unchecked again, you query the checkedstate first:
# Add Click-Event $Checkbox.add_CheckStateChanged({ If ($Checkbox.CheckState -eq "unchecked" ) { $Textbox.Clear() } else {$Textbox.Text = "IT WORKS"} })
Btw, this is the german forum, so please post in german or use the english forum, thanks!
Denniver
Blog: http://bytecookie.wordpress.com
Hilf mit und markiere hilfreiche Beiträge als "Hilfreich" und Beiträge die deine Frage ganz oder teilweise beantwortet haben als "Antwort".- Bearbeitet Denniver ReiningMVP, Moderator Mittwoch, 18. April 2012 02:53
- Als Antwort markiert Denniver ReiningMVP, Moderator Mittwoch, 18. April 2012 03:14
Alle Antworten
-
To add an event to an Winforms-object you need to use the "add_eventname()"-syntax :
# Add Click-Event $Checkbox.add_CheckStateChanged({$Textbox.Text = "IT WORKS"})
To delete the text again if the box is unchecked again, you query the checkedstate first:
# Add Click-Event $Checkbox.add_CheckStateChanged({ If ($Checkbox.CheckState -eq "unchecked" ) { $Textbox.Clear() } else {$Textbox.Text = "IT WORKS"} })
Btw, this is the german forum, so please post in german or use the english forum, thanks!
Denniver
Blog: http://bytecookie.wordpress.com
Hilf mit und markiere hilfreiche Beiträge als "Hilfreich" und Beiträge die deine Frage ganz oder teilweise beantwortet haben als "Antwort".- Bearbeitet Denniver ReiningMVP, Moderator Mittwoch, 18. April 2012 02:53
- Als Antwort markiert Denniver ReiningMVP, Moderator Mittwoch, 18. April 2012 03:14
-
Just saw that you posted that in the english forum as well (and already got it answered). Don't do that!
Blog: http://bytecookie.wordpress.com
Hilf mit und markiere hilfreiche Beiträge als "Hilfreich" und Beiträge die deine Frage ganz oder teilweise beantwortet haben als "Antwort".