【问题标题】:How to put a ContextMenu into the header of a TabPage如何将 ContextMenu 放入 TabPage 的标题中
【发布时间】:2013-06-04 17:25:21
【问题描述】:

我有一个自定义的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);
            }
        }
    }
}

如果我将MouseUp 绑定到TabControl,我会在整个TabPage 中得到ContextMenu。如果我将它绑定到TabPage,我只会在正文中而不是在标题中获得ContextMenu

有没有办法让 ContextMenu 只显示在标题 Click 上?

【问题讨论】:

  • MouseUp 事件是连接到每个 TabPage,还是连接到 TabControl
  • MouseUp 连接到 TabControl。如果我将它连接到 TabControl,我会在整个标签页中获得 ContextMenu。如果我将它绑定到 TabPage,我只会在正文中而不是在标题中获得 ContextMenu。

标签: c# events contextmenu tabcontrol


【解决方案1】:

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

}

【讨论】:

  • 只是出于好奇,不将 ContextMenu 分配给任何东西的原因是什么?
  • 如果它没有被分配,那么它就不能在你不想要的时候出现。由于您是在运行时动态创建菜单,因此它不需要存储在任何地方...
【解决方案2】:

除了像 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;
                }
            }
        }
    }

它完全一样,但不会在您的工具箱中添加额外的控件:) 如果您想在多个 TabControl 上使用它,也可以使其通用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-01-20
    • 2011-07-17
    • 2013-01-07
    相关资源
    最近更新 更多