如何通过按Powershell中的Enter键来调用按钮单击

时间:2015-09-15 01:28:01

标签: forms powershell button enter

我有一个由CSV文件填充的下拉列表。列表旁边是一个“开始”按钮,它根据所选内容将CSV信息放入程序的其余部分。当选择一个值时,我希望能够按Enter而不是单击Go并按Enter键基本上调用按钮单击。我正确地想到了吗?我找到了一些东西,但似乎都没有。可能是因为我不知道在我的代码中把它放在哪里。

这是下拉菜单和按钮代码......

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 120
$System_Drawing_Size.Height = 20
$label5.Size = $System_Drawing_Size
$label5.Text = "Company Presets:"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 223
$System_Drawing_Point.Y = 18 #545
$label5.Location = $System_Drawing_Point
$label5.DataBindings.DefaultDataSourceUpdateMode = 0
$label5.Name = "label5"
$label5.BackColor = "Transparent"

$form1.Controls.Add($label5)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 225
$System_Drawing_Point.Y = 46 #569
$companybox.Location = $System_Drawing_Point
$companybox.DataBindings.DefaultDataSourceUpdateMode = 0
$companybox.FormattingEnabled = $True
$companybox.Name = "companybox"
$companybox.TabIndex = 18
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 260
$companybox.Size = $System_Drawing_Size
$companybox.DropDownHeight = 125

ForEach ($Items in $List) {

    $companybox.Items.Add($Items)

}

$companybox.AutoCompleteSource = 'CustomSource'
$companybox.AutoCompleteMode='SuggestAppend'
$companybox.AutoCompleteCustomSource=$autocomplete
$List | % {$companybox.AutoCompleteCustomSource.AddRange($_) }

$Form1.Controls.Add($companybox)

$gobutton.TabIndex = 20
$gobutton.Name = "Go"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 34
$System_Drawing_Size.Height = 23
$gobutton.Size = $System_Drawing_Size
$gobutton.UseVisualStyleBackColor = $True
$gobutton.Text = "Go"
$gobutton.ForeColor = "Black"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 490
$System_Drawing_Point.Y = 44 #567
$gobutton.Location = $System_Drawing_Point
$gobutton.DataBindings.DefaultDataSourceUpdateMode = 0
$gobutton.add_Click($handler_gobutton_Click)

$form1.Controls.Add($gobutton)

以下是我尝试过的三件事。我把这些块放在$ companybox部分,而$ gobutton部分似乎都没有用。我每次都更改了$ textbox变量,尝试将& $ buttongo_click更改为$ handler_gobutton_click和$ gobutton_click。

$textboxpath_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{ 
if ($_.KeyChar -eq [System.Windows.Forms.Keys]::Enter) { 
&$buttonGo_Click 
} 
}

$textbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
if($_.KeyChar -eq 13){
    [void][System.Windows.Forms.MessageBox]::Show('Enter key entered'+$_.KeyChar)
}
}

$textbox1_KeyUp=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if($_.KeyCode -eq 'Enter')
{
    &$button1_Click
}
}

谢谢!

1 个答案:

答案 0 :(得分:5)

将Form的AcceptButton属性设置为Go按钮,例如:

$form1.AcceptButton = $goButton