如何允许Ctrl + A在Windows窗体文本框中选择全部PowerShell

问题描述

我正在用Windows窗体GUI编写.ps1脚本。当我从Powershell ISE运行它时,它允许使用Ctrl + A在文本框中“全选”。但是,在ISE外部运行.ps1时,CTRL + A的操作无效。

您知道我可以在文本框中更改哪些设置以允许Ctrl + A的设置吗? 我在该主题上唯一能找到的线程是用C等其他语言编写的。

目前我所拥有的:

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(110,20)
$textBox.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") {$okButton.PerformClick()}
    })
**$textBox.acceptstab = $true
$textBox.shortcutsenabled = $True**
$form.Controls.Add($textBox)

解决方法

我可以在这里重现您的问题。

一种选择是合并此答案-https://stackoverflow.com/a/29957334/3156906-并致电Application.EnableVisualStyles()

然后您的示例变成了(带有一些附加的设置代码以使它成为独立的):

Add-Type -AssemblyName "System.Windows.Forms"
Add-Type -AssemblyName "System.Drawing"

[System.Windows.Forms.Application]::EnableVisualStyles()

$form = new-object System.Windows.Forms.Form

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(110,20)
$textbox.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") {$okButton.PerformClick()}
    })
#$textbox.acceptstab = $true
#$textbox.ShortcutsEnabled = $true

$form.Controls.Add($textBox)

$form.ShowDialog()

然后您可以在文本框中使用 Ctrl + A 来选择文本。

,

您可以手动编写Ctrl + A事件以选择文本框内容:

$textbox.Add_KeyDown({
  if (($_.Control) -and ($_.KeyCode -eq 'A')) {
     $textbox.SelectAll()
  }
})