Forms 如何关闭主窗体并使用PowerShell调用另一个窗体?

Forms 如何关闭主窗体并使用PowerShell调用另一个窗体?,forms,powershell,Forms,Powershell,我有两张表格。1是主窗体,2是子窗体。在主窗体中,我使用计时器和按钮。单击按钮后,将显示第二个窗体,主窗体将关闭。但我试过了,主窗体关闭了,但计时器仍在计数。因此,一旦计时器超时,第二个窗体将自动关闭。 我的期待一旦主形态,计时器也停止了。任何人都可以告诉我。多谢各位 function JobHandlingFunction { # Job Handling Switch Configuration function SwitchConfig {

我有两张表格。1是主窗体,2是子窗体。在主窗体中,我使用计时器和按钮。单击按钮后,将显示第二个窗体,主窗体将关闭。但我试过了,主窗体关闭了,但计时器仍在计数。因此,一旦计时器超时,第二个窗体将自动关闭。 我的期待一旦主形态,计时器也停止了。任何人都可以告诉我。多谢各位

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()
    

这两个表单都被称为
$MainForm
,计时器将关闭
$MainForm
$MainForm.close()
)。重命名第二个
$MainForm
将解决您的问题

但是,第二个
$MainForm
是在另一个函数中创建的,应该只存在于该函数的范围内。所以真正的问题是:为什么父作用域中的
$MainForm.Close()
会关闭子作用域的
$MainForm