我有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] 删除。
我来说两句