[英]Powershell (studio) - AutoComplete dropdown value clicks a button?
I'm building a GUI for a powershell script. 我正在为Powershell脚本构建GUI。 It's a form built in Powershell studio. 这是Powershell Studio中内置的表单。
I have this code which works well: 我有很好的这段代码:
(Get-Content ".\historique.txt") | ? { $_.trim() -ne "" } | set-content ".\historique.txt"
$postes_historique = Get-Content ".\historique.txt"
$textboxPoste.AutoCompleteCustomSource.AddRange($postes_historique)
It takes what is in the "historique.txt" text file and suggests autocomplete values for the textbox like this: 它采用“ historique.txt”文本文件中的内容,并为文本框建议自动完成值,如下所示:
On that texbox, i have a KEYDOWN event set up so when a user presses ENTER it clicks the button below the textbox: 在该texbox上,我设置了KEYDOWN事件,因此当用户按下ENTER键时,它单击了文本框下方的按钮:
$textboxPoste_KeyDown = [System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if ($_.KeyCode -eq 'Enter')
{
$buttonConnexion.PerformClick()
}
}
The strange part and my question is : 奇怪的是,我的问题是:
-If I click a value in the dropdown, it clicks the button. -如果我在下拉菜单中单击一个值,则会单击按钮。 (UNWANTED BEHAVIOR) (不良行为)
-If I REMOVE that KeyDown enter event, it DOESN'T click the button. -如果我删除了KeyDown输入事件,则不要单击该按钮。 (Wanted behavior) (想要的行为)
That seems very strange to me, it's as if when you click a dropdown value, the code understands it like "YOU PRESSED ENTER". 这对我来说似乎很奇怪,好像您单击一个下拉值时,代码将其理解为“ YOU PRESSED ENTER”一样。 What kind of weird correlation is that? 那是什么奇怪的关联? I want to have both, which is being able to press down enter to click the button AND being able to choose an autocomplete value without it triggering a button click...Doesn't seem like too much to ask, no? 我想同时拥有这两种功能,既可以按下Enter键单击该按钮,又可以选择一个自动完成值,而不会触发按钮单击。
Thank you for your time. 感谢您的时间。
Not sure if there is another solution but the work around in this case is : 不知道是否还有其他解决方案,但是在这种情况下的解决方法是:
REMOVE the keydown event that clicks the button when ENTER is pressed 删除按下ENTER键时单击按钮的击键事件
and 和
REPLACE it by setting the form's AcceptButton to your button. 通过将表单的AcceptButton设置为按钮来替换它。 This way when user presses ENTER, that button is triggered and the autocomplete dropdown acts as expected. 这样,当用户按下ENTER键时,该按钮将被触发,并且自动完成下拉菜单将按预期方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.