1

Im trying to build a gui form using powershell, i want to add a button

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
$Form.Controls.Add($Button)
$Button.Add_Click($Button_Click)

now i want to create a function called button_click with a few commands, lets say: echo "Hello world" so i write this:

Function Button_Click(){
echo "Hello World"}

But clicking on the button wont give me any result, what am i doing wrong here?

2
  • 3
    Call $Button.Add_Click($function:Button_Click) after the function has been defined Commented May 8, 2020 at 13:34
  • Why write this to a console, if you really are using a GUI? Just add the text to the form element or add a UI element (textbox, Listbox, etc.) to write to. Commented May 8, 2020 at 16:44

2 Answers 2

5

You were using Write-Output which will write to the pipeline and in this case will not be displayed because your PowerShell console is locked while the $form.ShowDialog() is active.

However you can still do this! Write-host is another cmdlet for returning output and it can directly write to the PowerShell host window in real time. This is one of those rare times when you probably do want to use Write-Host.

Then, some small tweaks to make. Your event handler behavior should generally be defined before adding the $button to the $Form.

You made a function named Button_click, but you add the event handler like it is a variable. Here's how to do that instead, by making a variable which contains a {scriptblock}:

$button_click = {write-host "hi Sahar"}

And with that done, your code should look like this, and it will work as expected

$form = New-Object System.Windows.Forms.Form
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
$Button.Add_Click($Button_Click)

$Form.Controls.Add($Button)


 $form.showdialog()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

One could have used --- $button_click = {$env:USERNAME | Out-Host} --- as wellm thus avoiding the Write-* stuff.
Remember to vote up and mark as answer if a response helped :)
0

need add {}

$Button.Add_Click({$Button_Click})

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
It work $Button.Add_Click({Add-Type -AssemblyName System.Windows.Forms;[Windows.Forms.MessageBox]::Show('My Message');})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.