简体   繁体   English

如何将ContextMenu放入TabPage的标题中

[英]How to put a ContextMenu into the header of a TabPage

I have a custom TabControl in which I have TabPages with ContextMenu bound to them. 我有一个自定义TabControl ,其中我有TabPagesContextMenu绑定到它们。

I want the menu to show up only when the page header is being clicked. 我希望菜单仅在单击页眉时显示。

What I do is that, when the TabControl is clicked, I check these conditions: 我所做的是,当单击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);
            }
        }
    }
}

If I bind MouseUp to the TabControl , I get the ContextMenu in the entire TabPage . 如果我将MouseUp绑定到TabControl ,我会在整个TabPage获取ContextMenu If I bind it to the TabPage , I only get the ContextMenu in the body and not in the header. 如果我将它绑定到TabPage ,我只在正文中获取ContextMenu而不在标题中。

Is there a way to have a ContextMenu to show up only on header Click ? 有没有办法让ContextMenu只显示在标题点击?

Just don't ever assign the ContextMenu to anything...simply display it: 只是不要将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);
    }

}

Instead of override like Idle_Mind said, you could also do the same with a normal tabcontrol on the mouseevent: 而不是像Idle_Mind所说的那样覆盖,你也可以对mouseevent上的普通tabcontrol做同样的事情:

    private void tabControl1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            for (int i = 0; i < tabControl1.TabCount; ++i)
            {
                Rectangle r = tabControl1.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(tabControl1, e.Location);
                    break;
                }
            }
        }
    }

It does exactly the same, but doesn't add an extra control in your toolbox:) You could also make it generic if you want to use it on multiple TabControls. 它完全相同,但不会在工具箱中添加额外的控件:)如果要在多个TabControl上使用它,也可以使它成为通用的。

    private void showContextMenu_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            TabControl tabControl1 = sender as TabControl;
            [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM