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

丁苯橡胶

我有2个表格。1是主要形式,第二是子形式。在主要形式中,我使用计时器和按钮。单击按钮后,将显示第二个窗体,并且主窗体将关闭。但是我尝试了一下,主窗体关闭了,但是计时器仍在计数。因此,一旦计时器用完,第二个表格将自动关闭。我的期望一旦成为主要形式,计时器也将停止。任何人都可以给我想法。谢谢

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子作用域的...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在窗体上方打开另一个窗体后如何关闭窗体

来自分类Dev

如何处理另一个窗体的窗体关闭事件(VB.Net)

来自分类Dev

如何从另一个仍打开的窗体中检测何时关闭窗体?

来自分类Dev

关闭另一个窗体时关闭一个窗体

来自分类Dev

如何访问子窗体上的文本框控件的值,其中子窗体本身只是主窗体的另一个子窗体上的控件

来自分类Dev

在 C# 中从另一个类调用主窗体中的函数

来自分类Dev

如何从另一个类编辑主窗体中的checkedListBox?

来自分类Dev

关闭另一个MDI子窗体后,启用MDI子窗体的TButton

来自分类Dev

Outlook插件:关闭一个窗体并打开另一个窗体会在另一个窗体之间

来自分类Dev

从另一个窗体打开新的Windows窗体

来自分类Dev

如何在VBA中使用另一个宏/函数(UDF)调用对象/宏/用户窗体?

来自分类Dev

如何使用委托将活动窗体中的值获取到UserControl上的另一个窗体

来自分类Dev

如何使从另一个窗体视图打开的窗体视图在odoo中只读

来自分类Dev

如何从另一个窗体调整Windows窗体的大小

来自分类Dev

如何使从另一个窗体视图打开的窗体视图在odoo中只读

来自分类Dev

我如何使用文件上载来更改另一个Web窗体上的图像

来自分类Dev

从一个文件调用另一个用户窗体

来自分类Dev

关闭子窗体,然后单击C#.net中的用户控件中的按钮,打开另一个窗体?

来自分类Dev

当我关闭另一个窗体时如何在文本框中设置焦点?

来自分类Dev

在另一个仍然打开的窗体上调用方法?

来自分类Dev

ASP.NET Web 窗体从另一个 aspx 调用嵌入的 VB 代码。页

来自分类Dev

如何从另一个模式对话框窗体传递多个信息到MDI子窗体

来自分类Dev

如何从已经在窗体中的另一个 UserControl 事件中将单例 UserControl 加载到窗体

来自分类Dev

如何关闭另一个类的主窗口?

来自分类Dev

WinForms从另一个窗体引发事件

来自分类Dev

在另一个类的窗体上添加QGraphicsView

来自分类Dev

如何使用bindingSource将数据行所选对象的属性读取到另一个Windows窗体中

来自分类Dev

根据另一个窗体的当前位置设置Windows窗体的屏幕位置

来自分类Dev

将值从子窗体传递到另一个子窗体

Related 相关文章

  1. 1

    在窗体上方打开另一个窗体后如何关闭窗体

  2. 2

    如何处理另一个窗体的窗体关闭事件(VB.Net)

  3. 3

    如何从另一个仍打开的窗体中检测何时关闭窗体?

  4. 4

    关闭另一个窗体时关闭一个窗体

  5. 5

    如何访问子窗体上的文本框控件的值,其中子窗体本身只是主窗体的另一个子窗体上的控件

  6. 6

    在 C# 中从另一个类调用主窗体中的函数

  7. 7

    如何从另一个类编辑主窗体中的checkedListBox?

  8. 8

    关闭另一个MDI子窗体后,启用MDI子窗体的TButton

  9. 9

    Outlook插件:关闭一个窗体并打开另一个窗体会在另一个窗体之间

  10. 10

    从另一个窗体打开新的Windows窗体

  11. 11

    如何在VBA中使用另一个宏/函数(UDF)调用对象/宏/用户窗体?

  12. 12

    如何使用委托将活动窗体中的值获取到UserControl上的另一个窗体

  13. 13

    如何使从另一个窗体视图打开的窗体视图在odoo中只读

  14. 14

    如何从另一个窗体调整Windows窗体的大小

  15. 15

    如何使从另一个窗体视图打开的窗体视图在odoo中只读

  16. 16

    我如何使用文件上载来更改另一个Web窗体上的图像

  17. 17

    从一个文件调用另一个用户窗体

  18. 18

    关闭子窗体,然后单击C#.net中的用户控件中的按钮,打开另一个窗体?

  19. 19

    当我关闭另一个窗体时如何在文本框中设置焦点?

  20. 20

    在另一个仍然打开的窗体上调用方法?

  21. 21

    ASP.NET Web 窗体从另一个 aspx 调用嵌入的 VB 代码。页

  22. 22

    如何从另一个模式对话框窗体传递多个信息到MDI子窗体

  23. 23

    如何从已经在窗体中的另一个 UserControl 事件中将单例 UserControl 加载到窗体

  24. 24

    如何关闭另一个类的主窗口?

  25. 25

    WinForms从另一个窗体引发事件

  26. 26

    在另一个类的窗体上添加QGraphicsView

  27. 27

    如何使用bindingSource将数据行所选对象的属性读取到另一个Windows窗体中

  28. 28

    根据另一个窗体的当前位置设置Windows窗体的屏幕位置

  29. 29

    将值从子窗体传递到另一个子窗体

热门标签

归档