Cheries
Cheries

Reputation: 892

How to close main form and call another form with PowerShell?

I have 2 form. 1 is main form and 2nd is sub form. In the main form I use timer and button. Once I click the button, the 2nd form will show and the main form will close. But I tried it, the main form close, but the timer still counting. So the 2nd form will close automatically once the timer is out. My expectation once the main form, the timer also stop. Anyone can give me idea please. Thank you

function JobHandlingFunction
{
     # Job Handling Switch Configuration
     function SwitchConfig
     {
          param (

               $MainWidthSize, $MainHeightSize, $DeleteWidthSize, $DeleteHeightSize, $AddWidthSize,
               $AddheightSize, $PanelWidthSize, $PanelHeightSize, $TitleWidthSize, $TitleFontSize
          )
          
          # Main Form Configuration
          $MainWidth = $Width / $MainWidthSize
          $MainHeight = $Height / $MainHeightSize
          $MainFontSize = $Width / 30
          $MainForm.Size = New-Object System.Drawing.Size($MainWidth,$MainHeight)
          $MainFont = New-Object System.Drawing.Font("Calibri",$MainFontSize,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Pixel)
          $MainForm.Font = $MainFont
          $MainForm.Controls.Add($Panel)

          # Delete Button Configuration          
          $DeleteButtonWidth = $MainWidth / $DeleteWidthSize
          $DeleteButtonHeight = $MainHeight / $DeleteHeightSize
          $DeleteButton.Font = $MainFont
          $DeleteButton.Location = New-Object System.Drawing.Size($DeleteButtonWidth,$DeleteButtonHeight)
          $DeleteButton.AutoSize = $true
          $DeleteButton.FlatStyle = 1

          # Add Button Configuration
          $AddButton.Font = $MainFont
          $AddButtonWidth = $MainWidth / $AddWidthSize
          $AddButtonHeight = $MainHeight / $AddheightSize
          $AddButton.AutoSize = $true
          $AddButton.Location = New-Object System.Drawing.Size($AddButtonWidth,$AddButtonHeight)
          $AddButton.FlatStyle = 1

          # Panel Configuration
          $Panel.Controls.AddRange(@($DeleteButton, $AddButton, $Label, $Title))
          $Panel.Location = New-Object System.Drawing.Size($PanelWidthSize,$PanelHeightSize)

          # Title Configuration
          $TitleWidth = $MainWidth / $TitleWidthSize
          $TitleFontWidthLocation = $Width / $TitleFontSize
          $TitleFont = New-Object System.Drawing.Font("Calibri",$TitleWidth,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Pixel)
          $Title.Font = $TitleFont
          $Title.Location = New-Object System.Drawing.Size($TitleFontWidthLocation,50)
          $Title.AutoSize = $true

          # Label Configuration
          $LabelWidthLocation = $MainWidth / 2.4
          $Label.Location = New-Object System.Drawing.Size($LabelWidthLocation,200)
          $Label.Font = $TitleFont
          $Label.AutoSize = $true
     }

     #--[ Main Script Job Handling Switch ]--#

     # Main Form Intialize
     $MainForm = New-Object System.Windows.Forms.Form    
     $MainForm.MaximizeBox = $false
     $MainForm.text = "Job Handler Function"
     $MainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
     $MainForm.StartPosition = "CenterScreen"
     $MainForm.SizeGripStyle = 'Hide'    
     $MainForm.BackColor = "#fcfcfc"
     $Width = [System.Windows.Forms.Screen]::AllScreens.bounds.width
     $Height = [System.Windows.Forms.Screen]::AllScreens.bounds.Height
     $Height = $Height[0]
     $width = $Width[0]

     # Delete Button Initialize
     $DeleteButton = New-Object 'System.Windows.Forms.Button'
     $DeleteButton.BackColor               = "#0095d9"
     $DeleteButton.ForeColor               = "#fcfcfc"
     $DeleteButton.AutoSize = $true
     $DeleteButton.Text = "DELETE"
     $DeleteButton.Add_Click(
     {
          # Job Handling - Delete

          $MainForm.Dispose()         
     }
     )

     # Add Button Initialize
     $AddButton = New-Object 'System.Windows.Forms.Button'
     $AddButton.BackColor               = "#0095d9"
     $AddButton.ForeColor               = "#fcfcfc"
     $AddButton.AutoSize = $true
     $AddButton.Text = "   ADD  "
     $AddButton.Add_Click(
     {
          Write-Host "Continue the process"
          $Form.Dispose()
     }
     )

     # Panel Configuration
     $Panel = New-Object System.Windows.Forms.Panel
     $Panel.AutoSize = $true
     $Panel.BackColor  = "#fcfcfc"

     # Title Configuration
     $Title = New-Object 'System.Windows.Forms.Label'
     $Title.Text = "       Please Choose The Function`n------------------------------------------------"
     $Title.ForeColor = "#0590d0"
     $Title.AutoSize = $true

     # Label Configuration
     $Label = New-Object 'System.Windows.Forms.Label'
     $Label.ForeColor = "#fcfcfc"
     $Label.AutoSize = $True

     # Setting Interface Size
     if($Height -le "768")
     {
          SwitchConfig "3" "2" "-1" "-10" "17.5" "51.5" "4.6" "1.299" "1.4"
     }
     elseif ($Height -gt "768" -and $Height -lt "992"){
          SwitchConfig "3" "2" "-1" "-10" "17.5" "51.5" "4.5" "1.299" "1.6"
     }
     elseif ($Height -gt "992" -and $Height -lt "1200") {

          SwitchConfig "2" "1.5" "2.7" "1.8" "2.69" "2.7" "0" "-10" "20" "18"
     }
     elseif ($Height -gt "1200") {

          SwitchConfig "3" "2" "-1" "-10" "17.5" "51.5" "4.5" "1.299" "1.6"
     }
     else {

          Write-Host "Cannot define the height"
          SwitchConfig "3" "2" "-1" "-10" "17.5" "51.5" "4.5" "1.299" "1.6"
     }

     # Show The Interface
     [void] $MainForm.ShowDialog()
     #$MainForm.Dispose()
}

Function JobHandlerSwitch
{     
     param (

          $MainWidthSize, $MainHeightSize, $PanelWidthSize, $PanelHeightSize, 
          $TitleWidthSize, $TitleFontSize, $YesWidthSize, $YesHeightSize, $NoWidthSize
     )
     
     # Main Form Configuration
     $MainWidth = $Width / $MainWidthSize
     $MainHeight = $Height / $MainHeightSize
     $MainFontSize = $Width / 30
     $MainForm.Size = New-Object System.Drawing.Size($MainWidth,$MainHeight)
     $MainFont = New-Object System.Drawing.Font("Calibri",$MainFontSize,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Pixel)
     $MainForm.Font = $MainFont
     $MainForm.Controls.Add($Panel)     
     $MainForm.add_Load($Form_Load)

     # Panel Configuration
     $Panel.Controls.AddRange(@($YesButton, $NoButton, $Label, $Title))
     $Panel.Location = New-Object System.Drawing.Size($PanelWidthSize,$PanelHeightSize)

     # Title Configuration
     $TitleWidth = $MainWidth / $TitleWidthSize
     $TitleFontWidthLocation = $Width / $TitleFontSize
     $TitleFont = New-Object System.Drawing.Font("Calibri",$TitleWidth,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Pixel)
     $Title.Font = $TitleFont
     $Title.Location = New-Object System.Drawing.Size($TitleFontWidthLocation,50)
     $Title.AutoSize = $true

     # Yes Button Configuration          
     $YesButtonWidth = $MainWidth / $YesWidthSize
     $YesButtonHeight = $MainHeight / $YesHeightSize
     $YesButton.Font = $MainFont
     $YesButton.Location = New-Object System.Drawing.Size($YesButtonWidth,$YesButtonHeight)
     $YesButton.AutoSize = $true
     $YesButton.FlatStyle = 1

     # No Button Configuration
     $NoButton.Font = $MainFont
     $NoButtonWidth = $MainWidth / $NoWidthSize
     $NoButtonHeight = $MainHeight / $YesHeightSize
     $NoButton.AutoSize = $true
     $NoButton.Location = New-Object System.Drawing.Size($NoButtonWidth,$NoButtonHeight)
     $NoButton.FlatStyle = 1

     # Label Configuration
     $LabelWidthLocation = $MainWidth / 2.4
     $Label.Location = New-Object System.Drawing.Size($LabelWidthLocation,200)
     $Label.Font = $TitleFont
     $Label.AutoSize = $true

}

     # Main Form Intialize
     $MainForm = New-Object System.Windows.Forms.Form    
     $MainForm.MaximizeBox = $false
     $MainForm.text = "Job Handler Switch"
     $MainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
     $MainForm.StartPosition = "CenterScreen"
     $MainForm.BackColor = "#0095D9"
     $Width = [System.Windows.Forms.Screen]::AllScreens.bounds.width
     $Height = [System.Windows.Forms.Screen]::AllScreens.bounds.Height
     $Height = $Height[0]
     $Width = $Width[0]

     # Yes Button Initialize
     $YesButton = New-Object 'System.Windows.Forms.Button'
     $YesButton.BackColor = "#fcfcfc"
     $YesButton.ForeColor = "#0095d9"
     $YesButton.AutoSize = $true
     $YesButton.Text = "Yes"
     $YesButton.Add_Click(
     {
          $MainForm.Hide()
          
          JobHandlingFunction
 
     }
     )

     # No Button Initialize
     $NoButton = New-Object 'System.Windows.Forms.Button'
     $NoButton.BackColor = "#fcfcfc"
     $NoButton.ForeColor = "#0095d9"
     $NoButton.AutoSize = $true
     $NoButton.Text = "No"
     $NoButton.Add_Click(
     {
          Write-Host "Continue the process"
          $MainForm.Close()

     }
     )

     # Label Initialize
     $Label = New-Object 'System.Windows.Forms.Label'
     $Label.ForeColor = "#fcfcfc"
     $Label.AutoSize = $True

     # Title Initialize
     $Title = New-Object 'System.Windows.Forms.Label'
     $Title.Text = "Do you need to handle the job?"
     $Title.ForeColor = "#fcfcfc"
     $Title.AutoSize = $true

     # Panel Initialize
     $Panel = New-Object System.Windows.Forms.Panel
     $Panel.AutoSize = $true
     $Panel.BackColor  = "#fffff"
     $Panel.BackColor = "#0095d9"

     # Timer Initialize
     $timer1 = New-Object 'System.Windows.Forms.Timer'
     
     # Initial Form WindowState
     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
     $Form_Load = { 

          $TotalTime = 10 
          $script:StartTime = (Get-Date).AddSeconds($TotalTime)
          $timer1.Start()
     }

     $Cancel_Click = {}
     $timer1_Tick = {

          [TimeSpan]$span = $script:StartTime - (Get-Date)
          $Label.Text = "{0:N0}" -f $span.TotalSeconds
          if ($span.TotalSeconds -le 0) {
               $timer1.Stop()
               $MainForm.Close()
          }
     }

     $Form_StateCorrection_Load = { $Form.WindowState = $InitialFormWindowState }

     $Form_Cleanup_FormClosed = {

          $MainForm.remove_Load($Form_Load)
          $timer1.remove_Tick($timer1_Tick)
          $MainForm.remove_Load($Form_StateCorrection_Load)
          $MainForm.remove_FormClosed($Form_Cleanup_FormClosed)  
     }

     $MainForm.SuspendLayout()
     $MainForm.add_Load($Form_Load)

     
     if($Height -le "768")
     {
          JobHandlerSwitch "3" "2" "-1" "-10" "17.5" "51.5" "4.6" "1.299" "1.4"
     }
     elseif ($Height -gt "768" -and $Height -lt "992"){
          JobHandlerSwitch "3" "2" "-1" "-10" "17.5" "51.5" "4.5" "1.299" "1.6"
     }
     elseif ($Height -gt "992" -and $Height -lt "1200") {
          JobHandlerSwitch "2" "1.5" "0" "-10" "17" "20" "4.3" "1.3" "1.6"
     }
     elseif ($Height -gt "1200") {
          JobHandlerSwitch "3" "2" "-1" "-10" "17.5" "51.5" "4.5" "1.299" "1.6"
     }
     else{

          Write-Host "Cannot define the height"
          JobHandlerSwitch "3" "2" "-1" "-10" "17.5" "51.5" "4.5" "1.299" "1.6"
     }

     $timer1.add_Tick($timer1_Tick)
     $MainForm.ResumeLayout()
     $MainForm.ShowDialog()
    

Upvotes: 1

Views: 1262

Answers (1)

T-Me
T-Me

Reputation: 1884

Both your forms are called $MainForm and the timer will close the $MainForm ($MainForm.Close()). Renaming the second $MainForm will solve your problem.

However the second $MainForm is created in another function and should only exist in the scope of that function. So the real question is: why does the $MainForm.Close() in the parent scope close the $MainForm of a child scope...

Upvotes: 1

Related Questions