I'm making a context menu strip appear during the right clicking of either a selected or unselected tab in the tab strip of a winforms tabcontrol. It's going to have close, and close all but this for now. Anyway, I need to be able to capture which tab the mouse is over when the right click is pressed. Anyone know how?
Another solution that I'd accept is one that selects the unselected tab with a right click before the context menu is shown.
In your mouse click event you can add this code to find it, if tabs
is your tabcontrol
for (int i = 0; i < tabs.TabCount; ++i) {
if (tabs.GetTabRect(i).Contains(e.Location)) {
//tabs.Controls[i]; // this is your tab
}
}
This could help, it captures the position of your right click with the mouse and if it's on the rectangle of any tab, that tab will be selected and get's right menu to be showed
private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
for (int i = 0; i < tabs.TabCount; ++i)
{
if (tabs.GetTabRect(i).Contains(e.Location))
{
tabControl1.SelectTab(i);
this.contextMenuStrip1.Show(this.tabControl1, e.Location);
}
}
}
}
Have fun :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With