winforms 添加_LostFocus和添加_GotFocus不起作用?

iswrvxsc  于 2022-11-16  发布在  Go
关注(0)|答案(1)|浏览(144)

我有一个包含多个TabPage的窗体。我希望只有当一个选项卡成为焦点时按钮才可见。下面是我的代码:

$form           = New-Object system.Windows.Forms.Form
$form.ClientSize    = New-Object System.Drawing.Point(350,380)
$form.text      = "Form"

$tabcontrol         = New-object System.Windows.Forms.TabControl
$tabcontrol.Size    = New-Object System.Drawing.Point(330,330)
$tabcontrol.Location    = New-Object System.Drawing.Point(10,10)
$form.Controls.Add($tabcontrol)

$tab0                         = New-object System.Windows.Forms.Tabpage
$tab0.DataBindings.DefaultDataSourceUpdateMode    = 0
$tab0.UseVisualStyleBackColor             = $True
$tab0.Text                    = "Tab0"
$tab0.AutoScroll                  = $true
$tabcontrol.Controls.Add($tab0)

$add_button           = New-Object System.Windows.Forms.Button
$add_button.Size      = New-Object System.Drawing.Size(23,23)
$add_button.Location  = New-Object System.Drawing.Point(10,350)
$add_button.Text      = "+"
$form.Controls.Add($add_button)

$remove_button            = New-Object System.Windows.Forms.Button
$remove_button.Size       = New-Object System.Drawing.Size(23,23)
$remove_button.Location   = New-Object System.Drawing.Point(43,350)
$remove_button.Text       = "-"
$form.Controls.Add($remove_button)

$tab0.add_lostFocus({
    $add_button.Hide()
    $remove_button.Hide()
})
$tab0.add_gotFocus({
    $add_button.Show()
    $remove_button.Show()
})

$tab1                         = New-object System.Windows.Forms.Tabpage
$tab1.DataBindings.DefaultDataSourceUpdateMode    = 0
$tab1.UseVisualStyleBackColor             = $True
$tab1.Text                    = "Tab1"
$tabcontrol.Controls.Add($tab1)

尽管有add_GotFocus$tab0上的add_LostFocus,当我在选项卡之间切换时,按钮仍然可见。
我做错了什么?我该如何做到这一点?

mnowg1ta

mnowg1ta1#

我相信您要查找的事件是EnterLeave

$tab0.Add_Leave({
    $add_button, $remove_button | ForEach-Object Hide
})

$tab0.Add_Enter({
    $add_button, $remove_button | ForEach-Object Show
})

相关问题