locked
Powershell Forms, open a new form and close the current form at the same time with button.add_click RRS feed

  • Question

  • Hello Everyone,

    For several hours I am playing around with the below testing script. I thing the problem occurs in Line 24:

    $OKbutton.Add_Click({if($OKbutton.DialogResult = [System.Windows.Forms.DialogResult]::OK) {$date.ShowDialog(),$form1.Close()}})

    When I click the OK button, the new Window with a Calendar appears but it does not close form1. My understanding is, that after I $date.ShowDialog() it stops there and won't continue to do the $form1.Close().

    What am I doing wrong ? (I am about to go insane ;) ) 

    Target is, that when I press the OK button in Window "Form" 1, that the Window with the Calendar opens and Windows "Forms 1" shall close.

    Can someone advice?

    Thank you very much for your help,

    Mike

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

    $form1 = New-Object System.Windows.Forms.Form
    $form1.Text = 'Form 1'
    $form1.Size = New-Object System.Drawing.Size(300,200)
    $form1.StartPosition = 'CenterScreen'

    $date = New-Object System.Windows.Forms.Form
    $date.Text = 'Date'
    $date.Size = New-Object System.Drawing.Size @(243,230)
    $date.StartPosition = 'CenterScreen'

    $calendar = New-Object System.Windows.Forms.MonthCalendar
    $calendar.ShowTodayCircle = $false
    $calendar.MaxSelectionCount = 1
    $date.Controls.Add($calendar)

    $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.Add_Click({if($OKbutton.DialogResult = [System.Windows.Forms.DialogResult]::OK) {$date.ShowDialog(),$form1.Close()}})
    $form1.AcceptButton = $OKButton
    $form1.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 = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form1.CancelButton = $CancelButton
    $form1.Controls.Add($CancelButton)



    $form1.ShowDialog()

    Sunday, December 2, 2018 4:59 AM

Answers

  • After form1 closes it returns OK to PowerShell.  Then open foorm2.  You cannot open form2 from form1 because it is out-of-scope and SHowDialog is modal.


    \_(ツ)_/

    • Marked as answer by mstgier Sunday, December 2, 2018 6:24 AM
    Sunday, December 2, 2018 6:19 AM

All replies

  • Can't be done. Opening a new form will prevent the current form from closing.  That is a restriction for forms in PowerShell.

    Write your code so that when the first form closes it opens the second form.

    Like this:

    if('Ok' -eq $form1.ShowDialog()){
       $form2.ShowDialog()
    }


    \_(ツ)_/

    Sunday, December 2, 2018 5:17 AM
  • Dear JRV,

    thanx, a lot for the help as always.

    I am sorry, I do not understand. In your example, you are using twice ShowDialog?

    And you mention closing the first form before opening the second one and that I do understand but I do not understand,

    how I use it with "if" because  I can do only one thing at the time, correct?

    If (OK button is pressed) {then close the form.}. 

    So where do I have a wrong understanding?

    Thank you a lot for your support,

    Mike


    Sunday, December 2, 2018 6:15 AM
  • After form1 closes it returns OK to PowerShell.  Then open foorm2.  You cannot open form2 from form1 because it is out-of-scope and SHowDialog is modal.


    \_(ツ)_/

    • Marked as answer by mstgier Sunday, December 2, 2018 6:24 AM
    Sunday, December 2, 2018 6:19 AM