PowerShell winforms Tab选择索引已更改事件

时间:2016-10-19 10:49:23

标签: winforms powershell-v3.0

我使用PowerShell中的WinForms设计了GUI,如下所示

Add-Type -Assembly 'System.Windows.Forms'

$form = New-Object Windows.Forms.Form

$TabControl = New-Object System.Windows.Forms.TabControl

$tabPage1 = New-Object System.Windows.Forms.TabPage
$tabPage1.Name = "Tab1"
$tabPage1.Text = "Tab1"
$tabPage1.Width = 500
$tabPage1.Height = 500

$tabPage2 = New-Object System.Windows.Forms.TabPage
$tabPage2.Name = "Tab2"
$tabPage2.Text = "Tab2"
$tabPage2.Width = 500
$tabPage2.Height = 500


$TabControl.TabPages.Add($tabPage1)
$TabControl.TabPages.Add($tabPage2) 

$TabControl.SelectedIndexChanged($TabControl_SelectedIndexChanged)

$TabControl_SelectedIndexChanged
{
    ({$selectedTab = $TabControl.SelectedTab;

    [System.Windows.Forms.MessageBox]::Show($selectedTab)})
}
$form.Controls.Add($TabControl)

我想处理selectedindex已更改的事件,但我不能,上面的代码出现以下错误

Method invocation failed because [System.Windows.Forms.TabControl] does not contain a method named 'SelectedIndexChanged'.所以有人可以帮助我

1 个答案:

答案 0 :(得分:0)

想出这就是我需要添加事件的方式

而不是

TabControl.SelectedIndexChanged($TabControl_SelectedIndexChanged)

$TabControl_SelectedIndexChanged
{
    ({$selectedTab = $TabControl.SelectedTab;

    [System.Windows.Forms.MessageBox]::Show($selectedTab)})
}

$TabControl.Add_SelectedIndexChanged({
    $selectedTab = $TabControl.SelectedTab
    [System.Windows.Forms.MessageBox]::Show($selectedTab.TabIndex)
})