【问题标题】:how to capture event when right clicking on tab using winforms in powershell如何在powershell中使用winforms右键单击选项卡时捕获事件
【发布时间】:2021-10-10 20:29:29
【问题描述】:

我想在右键单击选项卡时触发一个事件,不是选项卡显示的内容,而是tabs 本身。

到目前为止,我已经尝试过为 tabcontrol 使用 selected 事件:

$maintab.add_selected({do something here})

这仅在左键单击选项卡时触发

我也尝试了所有这些,它们似乎对 tabcontrol 或 tabpages 中的任何地方的点击都没有响应

$maintab.add_mouseUP({do something here})
$maintab.add_mouseDown({do something here})
$maintab.add_click({do something here})
$maintab.add_selecting({do something here})

我还尝试为已添加到 tabcontrol 的标签页捕获 mouseUP 事件:

$tabpage.add_mouseUp({do something here})

这仅在单击选项卡下方的内容区域时有效,而不是选项卡本身。

这可能吗?

【问题讨论】:

    标签: powershell winforms tabs event-handling right-click


    【解决方案1】:

    我认为在 maintab 控件上使用 MouseUP 事件是正确的。

    下面将向您展示如何检测这些选项卡中的哪些被点击:

    $maintab.Add_mouseUP({
        param($sender,$e)
        if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Right) {
            # this part checks if on of the actual tabs is right-clicked
            for ($i = 0; $i -lt $this.TabCount; $i++) {
                if ($this.GetTabRect($i).Contains($e.Location)) {
                    # do something here
                    Write-Host "Right-click on tab $($this.TabPages[$i].Text)"
                    break
                }
            }
        }
    })
    

    【讨论】:

    • 你知道这很奇怪,我在之前的测试中在我的程序中注释掉了这一行 $maintab.add_mouseUP({write-host object: $this.name event: $_.button}) .从技术上讲,我所做的只是取消评论它现在可以工作了。我确实先对 VSC 进行了一些更新和回滚,但我不知道这会如何影响它!无论哪种方式,感谢您为我指明正确的方向!
    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多