forms - 如何关闭主窗体并使用 PowerShell 调用另一个窗体?
问题描述
我有2个表格。1是主表格,2nd是子表格。在主窗体中,我使用计时器和按钮。单击按钮后,将显示第二个表单,并且主表单将关闭。但是我试了一下,主窗体关闭了,但是定时器还在计时。因此,一旦计时器结束,第二种形式将自动关闭。我的期望一旦主窗体,定时器也停止。任何人都可以给我想法。谢谢
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
子范围...
推荐阅读
- javascript - 如何添加 HTML 附加 javascript...“引号”问题
- apache-spark - Spark EMR 作业失败:原因:org.apache.spark.memory.SparkOutOfMemoryError: Unable to acquire 16384 bytes of memory, got 0
- c# - 带有包含空格和“/”的字符串的 Http 请求
- react-native - 如何在 react-native 中使用加速度计传感器找到方向?
- r - R:使用 id 和记录频率创建新的数据框
- active-directory - 如何修复具有静态 IP 地址的域网络上的“未识别网络”?
- r - 使用参考列总结数据框
- java - JFLEX 是否有替代 \b 和/或负前瞻的方法?
- python - 使用 Discord.py 存储每个服务器的设置
- webpack - 更改 WebPack Chunk | 的 chunk entry-module\deferred-modules Angular Builders WebPack 插件