how to capture event when right clicking on tab using winforms in powershell

Viewed 145

I want to fire an event when right clicking on a tab, not the contents displayed by the tab, but the tabs themselves.

So far, I have tried using the selected event for the tabcontrol:

$maintab.add_selected({do something here})

this only fires when left clicking on the tabs

I also tried all of these, they don't seem to respond to clicks anywhere within the tabcontrol or the 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})

I have also tried capturing the mouseUP event for the tabpages that have been added to the tabcontrol:

$tabpage.add_mouseUp({do something here})

This only works when clicking on the contents area below the tab, not the tabs themselves.

Is this even possible?

1 Answers

I think you're right with using the MouseUP event on the maintab control.

Below shows you how you can detect which of these tabs was clicked:

$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
            }
        }
    }
})
Related