locked
Close Form RRS feed

  • Question

  • I have a form and I need to CLEAN close it when clicking 'x' button. Please advise what code should be included in OnClick_Exit. My actual form is much more complicated.

    Function OnClick_Exit() {
        Write-Host 'Closed'
        # code to release any created in form
        # this is just a sample form
    }
    
    $form = New-Object System.Windows.Forms.Form
    $form.Size = '600, 400'
    $form.StartPosition = 'CenterScreen'
    $Form.Add_FormClosing({OnClick_Exit})
    
    $form.ShowDialog() | Out-Null

    Sunday, January 13, 2019 11:08 PM

All replies

  • I use code similar to below.

        $MyButton.Add_Click({
            # The user clicked the Exit button.
            $MyForm.DialogResult = "OK"
            $MyForm.Close()
        }
    
    I added the button MyButton to the form MyForm.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Sunday, January 13, 2019 11:18 PM
  • If we set "DialogResult" then "$form.Close()" is unnecessary and no event is required. Just set DialogResult on the button:

    $button.DialogResult = 'Ok'

    The is no way to alter the effect of the "X" system menu item. You can intercept the form in the form "Closing" event.

    $form.add_Closing({
         # your code
    })


    \_(ツ)_/

    Sunday, January 13, 2019 11:22 PM
  • Here is a minimal form showing how the button works:

    Add-Type -AssemblyName System.Windows.Forms
    $form = New-Object System.Windows.Forms.Form
    $buttonOk = New-Object System.Windows.Forms.Button
    $buttonOk.Dock = 'Fill'
    $buttonOk.Text = 'Click Me'
    $buttonOk.DialogResult = 'Ok'
    $form.Controls.Add($buttonOk)
    $form.ShowDialog()

    A form with better usefulness:

    # create all controls and add to form Add-Type -AssemblyName System.Windows.Forms $form = New-Object System.Windows.Forms.Form $buttonOk = New-Object System.Windows.Forms.Button
    $form.Controls.Add($buttonOk) # configure form $form.StartPosition = 'CenterScreen' $form.ControlBox = $false # configure button $buttonOk.Text = 'Click Me' $buttonOk.DialogResult = 'Ok' $form.ShowDialog()



    \_(ツ)_/



    • Edited by jrv Sunday, January 13, 2019 11:33 PM
    Sunday, January 13, 2019 11:32 PM
  • This demonstrates the full lifecycle plus how to auto-flow the controls on a form.

    # create all controls
    Add-Type -AssemblyName System.Windows.Forms
    $form = New-Object System.Windows.Forms.Form
    $flowPanel = New-Object System.Windows.Forms.FlowLayoutPanel
    $form.Controls.Add($flowPanel)
    $textbox1 = New-Object System.Windows.Forms.TextBox
    $flowPanel.Controls.Add($textbox1)
    $textbox2 = New-Object System.Windows.Forms.TextBox
    $flowPanel.Controls.Add($textbox2)
    $textbox3 = New-Object System.Windows.Forms.TextBox
    $flowPanel.Controls.Add($textbox3)
    $buttonOk = New-Object System.Windows.Forms.Button
    $flowPanel.Controls.Add($buttonOk)
    
    # configure form
    $form.StartPosition = 'CenterScreen'
    $form.ControlBox = $false
    
    # configure flow panel
    $flowPanel.FlowDirection = 'TopDown'
    $flowPanel.Dock = 'Fill'
    
    # configure button
    $buttonOk.Text = 'Click Me'
    $buttonOk.DialogResult = 'Ok'
    
    $form.add_Closing({
        Write-Host 'Executing closing code' -fore green
    })
    
    $form.ShowDialog()
    


    \_(ツ)_/

    Sunday, January 13, 2019 11:51 PM
  • My form does not have a button. I just want to click 'X' button on the upper-right-hand corner of the form window to close it.
    Sunday, January 13, 2019 11:51 PM
  • My form does not have a button. I just want to click 'X' button on the upper-right-hand corner of the form window to close it.

    Just add the closing event to the form.  No matter how it is closed the event will be called.


    \_(ツ)_/

    Sunday, January 13, 2019 11:56 PM
  • When I add $form.close() in the event, the form does not close when click X

    Function OnClick_Exit() {
        Write-Host 'Closed'
        $form.close()
        # code to release any created in form
        # this is just a sample form
    }
    
    $form = New-Object System.Windows.Forms.Form
    $form.Size = '600, 400'
    $form.StartPosition = 'CenterScreen'
    $Form.Add_FormClosing({OnClick_Exit})
    
    $form.ShowDialog() | Out-Null

    Monday, January 14, 2019 12:20 AM
  • As I posted above.  Add the clse event to the form.  "formclosing()" is not the event.  The event is
    "add_Closing()".

    Don't use a function to do this.  Just add the code to the closing event or add a scriptblock.  Using a function for events will hide the necessary arguments from the code.

    $form_ClosingEvent = {
        Write-Host 'Closed'
        # code to release any created in form
        # this is just a sample form
    }
    $form.add_Closing( $form_ClosingEvent )

    Or ---

    $form.add_Closing({
        Write-Host 'Closed'
        # code to release any created in form
        # this is just a sample form
    })

    To use functions the functions have to have this signature.

    function fname{
        Param(
            $sender,
            $eventArg
       )
    ….

    To call a function in an event the event code has to look like this:

    fname $this $_

     A script block is much easier because it already knows about $this and $_.  "$this" is always the "sender" or containing object and "$_" is always the event args sent to the object.


    \_(ツ)_/




    • Edited by jrv Monday, January 14, 2019 12:41 AM
    Monday, January 14, 2019 12:37 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, January 22, 2019 7:06 AM
  • I know the issues of having Powershell hold data in variables so I clear my variables at the start of my form as well as at the end. This way when my program starts everything is clean. 
    Thursday, January 24, 2019 9:28 PM
  • I know the issues of having Powershell hold data in variables so I clear my variables at the start of my form as well as at the end. This way when my program starts everything is clean. 

    Which has nothing at all to do with the question and is just a bad habit used by non-programmers to avoid learning  good code practices.

    Proper variable use is not hard if you work correctly with "scopes". 

    With forms the variables are all within the form but the best way to protect from accidental damage is to place the form in a function.  All variables will be removed when the function exits.  Also variables defined in events are not visible outside of the event.  The method for avoiding this is to either tag them onto the form  or the control.  We also use the "Tag" property to save variables.  Add an object and you can have as many variables as you want.


    \_(ツ)_/

    Thursday, January 24, 2019 10:31 PM