How do I find out what tab I'm right clicking on in winforms tabcontrol?
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 :)
The sender
parameter of the event handler usually gives you the object that you clicked on.
void whatever_OnClick(object sender, EventArgs e) {
var tab = sender as TabControlClassHere;
}
精彩评论