c# - 如何将 ContextMenu 放入 TabPage 的页眉

标签 c# events contextmenu tabcontrol

我有一个自定义的 TabControl,其中我有 TabPagesContextMenu 绑定(bind)到它们。

我希望菜单仅在单击页眉时显示。

我所做的是,当点击 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);
            }
        }
    }
}

如果我将 MouseUp 绑定(bind)到 TabControl,我将在整个 TabPage 中获得 ContextMenu .如果我将它绑定(bind)到 TabPage,我只会在正文中获得 ContextMenu,而不会在 header 中获得。

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

最佳答案

永远不要将 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);
    }

}

关于c# - 如何将 ContextMenu 放入 TabPage 的页眉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923904/

相关文章:

c# - 如何使用 linq 对嵌套集合进行排序?

javascript - jQuery,来自类的触发事件

javascript - CKEditor - 将上下文菜单项添加到图像

C# 覆盖默认的 ContextMenu 样式 (WPF)

java - 在其 TreeView 中显示 ITreeContentProvider 的 "input-element"

c# - DLL 中的 DoEvents

c# - 缓存辅助类是单例模式的候选者吗?

c# - 如果用户输入的服务器 IP 地址不正确,则显示错误消息

javascript - iOS7 Safari 手机| Html 视频流 |未触发结束事件

javascript - 我的 input 元素设置为禁用时如何接收 onclick 事件?