如何将ContextMenu放入TabPage的标题中

Fra*_*amp 6 c# events tabcontrol contextmenu

我有一个与TabControl我绑定的习惯.TabPagesContextMenu

我希望菜单只在单击页面标题时显示.

我所做的是,当TabControl点击它时,我检查这些条件:

private void MouseUp(object sender, MouseEventArgs e) 
{
    if (e.Button == Mousebuttons.Right) 
    {
        for (int i = 0; i < TabCount; ++i) 
        {
            Rectangle r = GetTabRect(i);
            if (r.Contains(e.Location) /* && it is the header that was clicked*/) 
            {
                // Change slected index, get the page, create contextual menu
                ContextMenu cm = new ContextMenu();
                // Add several items to menu
                page.ContextMenu = cm;
                page.ContextMenu.Show(this, e.Location);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我绑定MouseUpTabControl,我得到 ContextMenu了整个 TabPage.如果我将它绑定到 TabPage,我只会 ContextMenu在正文中而不是在标题中.

有没有办法让ContextMenu只显示在标题点击?

Idl*_*ind 8

只是不要将ContextMenu分配给任何东西......只需显示它:

public class MyTabControl : TabControl
{

    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            for (int i = 0; i < TabCount; ++i)
            {
                Rectangle r = GetTabRect(i);
                if (r.Contains(e.Location) /* && it is the header that was clicked*/)
                {
                    // Change slected index, get the page, create contextual menu
                    ContextMenu cm = new ContextMenu();
                    // Add several items to menu
                    cm.MenuItems.Add("hello");
                    cm.MenuItems.Add("world!");
                    cm.Show(this, e.Location);
                    break;
                }
            }
        }
        base.OnMouseUp(e);
    }

}
Run Code Online (Sandbox Code Playgroud)