gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:09:25 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com