PowerShell WinForms表单求助:默认选中项与自动确认倒计时
嘿,我已经帮你搞定了这两个需求,下面是修改后的完整PowerShell WinForms代码,我给你理清楚关键改动点:
改动说明
- 默认选中指定项:在窗体加载事件里,用
SetItemChecked方法给两个CheckedListBox设置默认选中的条目(索引从0开始计数,所以item1是索引0,item3是索引2,item4是索引0)。 - 10秒自动确认倒计时:添加了一个Timer控件,设置10秒后自动触发OK按钮的点击;同时给所有用户可能操作的控件(包括两个复选列表、标签页、OK按钮、窗体本身)加了操作事件,只要用户有任何鼠标或键盘操作,就立刻停止倒计时,取消自动确认。
完整代码
#---------------------------------------------- #region Application Functions #---------------------------------------------- #endregion Application Functions #---------------------------------------------- # Generated Form Function #---------------------------------------------- function Show-form_psf { #---------------------------------------------- #region Import the Assemblies #---------------------------------------------- [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') #endregion Import Assemblies #---------------------------------------------- #region Generated Form Objects #---------------------------------------------- [System.Windows.Forms.Application]::EnableVisualStyles() $form1 = New-Object 'System.Windows.Forms.Form' $tabcontrol1 = New-Object 'System.Windows.Forms.TabControl' $tabpage1 = New-Object 'System.Windows.Forms.TabPage' $checkedlistbox1 = New-Object 'System.Windows.Forms.CheckedListBox' $tabpage2 = New-Object 'System.Windows.Forms.TabPage' $checkedlistbox2 = New-Object 'System.Windows.Forms.CheckedListBox' $buttonOK = New-Object 'System.Windows.Forms.Button' $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState' #endregion Generated Form Objects #---------------------------------------------- # User Generated Script #---------------------------------------------- # 定义停止倒计时的通用函数 function Stop-Countdown { if ($script:countdownTimer -and $countdownTimer.Enabled) { $countdownTimer.Stop() } } $form1_Load={ # 设置默认选中项 # tabpage1的checkedlistbox1选中item1(索引0)和item3(索引2) $checkedlistbox1.SetItemChecked(0, $true) $checkedlistbox1.SetItemChecked(2, $true) # tabpage2的checkedlistbox2选中item4(索引0) $checkedlistbox2.SetItemChecked(0, $true) # 初始化10秒倒计时Timer $script:countdownTimer = New-Object System.Windows.Forms.Timer $countdownTimer.Interval = 10000 # 1000毫秒=1秒,这里是10秒 $countdownTimer.add_Tick({ # 自动触发OK按钮点击 $buttonOK.PerformClick() $countdownTimer.Stop() }) $countdownTimer.Start() } #region Control Helper Functions function Update-ListBox { <# .SYNOPSIS This functions helps you load items into a ListBox or CheckedListBox. .DESCRIPTION Use this function to dynamically load items into the ListBox control. .PARAMETER ListBox The ListBox control you want to add items to. .PARAMETER Items The object or objects you wish to load into the ListBox's Items collection. .PARAMETER DisplayMember Indicates the property to display for the items in this control. .PARAMETER Append Adds the item(s) to the ListBox without clearing the Items collection. .EXAMPLE Update-ListBox $ListBox1 "Red", "White", "Blue" .EXAMPLE Update-ListBox $listBox1 "Red" -Append Update-ListBox $listBox1 "White" -Append Update-ListBox $listBox1 "Blue" -Append .EXAMPLE Update-ListBox $listBox1 (Get-Process) "ProcessName" .NOTES Additional information about the function. #> param ( [Parameter(Mandatory = $true)] [ValidateNotNull()] [System.Windows.Forms.ListBox] $ListBox, [Parameter(Mandatory = $true)] [ValidateNotNull()] $Items, [Parameter(Mandatory = $false)] [string] $DisplayMember, [switch] $Append ) if (-not $Append) { $listBox.Items.Clear() } if ($Items -is [System.Windows.Forms.ListBox+ObjectCollection]) { $listBox.Items.AddRange($Items) } elseif ($Items -is [Array]) { $listBox.BeginUpdate() foreach ($obj in $Items) { $listBox.Items.Add($obj) } $listBox.EndUpdate() } else { $listBox.Items.Add($Items) } $listBox.DisplayMember = $DisplayMember } #endregion $buttonOK_Click={ #TODO: Place custom script here $checkedlistbox1.CheckedItems | Out-Host $checkedlistbox2.CheckedItems | Out-Host } # 给所有可交互控件添加操作事件,触发停止倒计时 $checkedlistbox1.add_MouseDown({ Stop-Countdown }) $checkedlistbox1.add_KeyDown({ Stop-Countdown }) $checkedlistbox2.add_MouseDown({ Stop-Countdown }) $checkedlistbox2.add_KeyDown({ Stop-Countdown }) $tabcontrol1.add_SelectedIndexChanged({ Stop-Countdown }) $buttonOK.add_MouseEnter({ Stop-Countdown }) $form1.add_MouseDown({ Stop-Countdown }) $form1.add_KeyDown({ Stop-Countdown }) # --End User Generated Script-- #---------------------------------------------- #region Generated Events #---------------------------------------------- $Form_StateCorrection_Load= { #Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } $Form_Cleanup_FormClosed= { #Remove all event handlers from the controls try { $buttonOK.remove_Click($buttonOK_Click) $form1.remove_Load($form1_Load) $form1.remove_Load($Form_StateCorrection_Load) $form1.remove_FormClosed($Form_Cleanup_FormClosed) # 清理Timer资源 if ($script:countdownTimer) { $countdownTimer.Stop() $countdownTimer.Dispose() } } catch { Out-Null <# Prevent PSScriptAnalyzer warning #> } } #endregion Generated Events #---------------------------------------------- #region Generated Form Code #---------------------------------------------- $form1.SuspendLayout() $tabcontrol1.SuspendLayout() $tabpage1.SuspendLayout() $tabpage2.SuspendLayout() # # form1 # $form1.Controls.Add($tabcontrol1) $form1.Controls.Add($buttonOK) $form1.AcceptButton = $buttonOK $form1.AutoScaleDimensions = '6, 13' $form1.AutoScaleMode = 'Font' $form1.ClientSize = '284, 262' $form1.FormBorderStyle = 'FixedDialog' $form1.MaximizeBox = $False $form1.MinimizeBox = $False $form1.Name = 'form1' $form1.StartPosition = 'CenterScreen' $form1.Text = 'Form' $form1.add_Load($form1_Load) # # tabcontrol1 # $tabcontrol1.Controls.Add($tabpage1) $tabcontrol1.Controls.Add($tabpage2) $tabcontrol1.Location = '22, 17' $tabcontrol1.Name = 'tabcontrol1' $tabcontrol1.SelectedIndex = 0 $tabcontrol1.Size = '240, 204' $tabcontrol1.TabIndex = 1 # # tabpage1 # $tabpage1.Controls.Add($checkedlistbox1) $tabpage1.Location = '4, 22' $tabpage1.Name = 'tabpage1' $tabpage1.Padding = '3, 3, 3, 3' $tabpage1.Size = '232, 178' $tabpage1.TabIndex = 0 $tabpage1.Text = 'tabpage1' $tabpage1.UseVisualStyleBackColor = $True # # checkedlistbox1 # $checkedlistbox1.CheckOnClick = $True $checkedlistbox1.FormattingEnabled = $True [void]$checkedlistbox1.Items.Add('item 1') [void]$checkedlistbox1.Items.Add('item 2') [void]$checkedlistbox1.Items.Add('item 3') $checkedlistbox1.Location = '26, 27' $checkedlistbox1.Name = 'checkedlistbox1' $checkedlistbox1.Size = '181, 124' $checkedlistbox1.TabIndex = 0 $checkedlistbox1.UseCompatibleTextRendering = $True # # tabpage2 # $tabpage2.Controls.Add($checkedlistbox2) $tabpage2.Location = '4, 22' $tabpage2.Name = 'tabpage2' $tabpage2.Padding = '3, 3, 3, 3' $tabpage2.Size = '232, 178' $tabpage2.TabIndex = 1 $tabpage2.Text = 'tabpage2' $tabpage2.UseVisualStyleBackColor = $True # # checkedlistbox2 # $checkedlistbox2.CheckOnClick = $True $checkedlistbox2.FormattingEnabled = $True [void]$checkedlistbox2.Items.Add('item 4') [void]$checkedlistbox2.Items.Add('item 5') [void]$checkedlistbox2.Items.Add('item 6') $checkedlistbox2.Location = '26, 27' $checkedlistbox2.Name = 'checkedlistbox2' $checkedlistbox2.Size = '181, 124' $checkedlistbox2.TabIndex = 1 $checkedlistbox2.UseCompatibleTextRendering = $True # # buttonOK # $buttonOK.Anchor = 'Bottom, Right' $buttonOK.DialogResult = 'OK' $buttonOK.Location = '197, 227' $buttonOK.Name = 'buttonOK' $buttonOK.Size = '75, 23' $buttonOK.TabIndex = 0 $buttonOK.Text = '&OK' $buttonOK.UseCompatibleTextRendering = $True $buttonOK.UseVisualStyleBackColor = $True $buttonOK.add_Click($buttonOK_Click) $tabpage2.ResumeLayout() $tabpage1.ResumeLayout() $tabcontrol1.ResumeLayout() $form1.ResumeLayout() #endregion Generated Form Code #---------------------------------------------- #Save the initial state of the form $InitialFormWindowState = $form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($Form_StateCorrection_Load) #Clean up the control events $form1.add_FormClosed($Form_Cleanup_FormClosed) #Show the Form return $form1.ShowDialog() } #End Function #Call the form Show-form_psf | Out-Null
内容的提问来源于stack exchange,提问作者tianyi
相关产品推荐
相关产品推荐

