如何将键盘焦点设置到 Powershell 中的 TextBox [英] How to set keyboard focus to a TextBox in Powershell

查看:60
本文介绍了如何将键盘焦点设置到 Powershell 中的 TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在 powershell 中自动将键盘焦点设置到文本框.

I'm looking to find out how to automatically set the keyboard focus to a text box in powershell.

我有一个脚本,要求用户从下拉菜单中选择一个选项,然后根据该选择,他们必须输入某些参数.一切正常.

I have a script that asks the user to select an option from a dropdown menu, then based on that selection, they have to input certain parameters. That all works fine.

为了便于使用,我希望每次显示一个新的输入框时,键盘的焦点都转移到输入框上,这样用户就不必一直点击它来输入一些文本.

For ease of use, I'd like the focus of the keyboard to shift to the input box each time a new one is shown, so the user doesn't have to keep clicking on it to enter some text.

到目前为止我的代码:

function inputBox($parameter)
{
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = $parameter
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$paramValue=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})



$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,90)
$OKButton.Size = New-Object System.Drawing.Size(75,25)
$OKButton.Text = "OK"
$OKButton.Add_Click({$paramValue=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = $parameter
$objForm.Controls.Add($objLabel) 

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
$objForm.KeyPreview = $True
$objForm.Controls.Add($objTextBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

return $paramValue

有什么想法吗?

谢谢!

推荐答案

按照你的代码,我可以在其中找到一个下拉菜单,在显示表单时将焦点放在文本框上我已经完成了:

Following your code, where I can find a dropdown menu, to give focus to textbox when form is shown I've done:

$objForm.Add_Shown({$objForm.Activate(); $objTextBox.focus()})

这篇关于如何将键盘焦点设置到 Powershell 中的 TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆