ContextMenu 不显示,我不知道为什么

发布于 2024-08-02 20:44:55 字数 1822 浏览 2 评论 0原文

我正在编写一个 C# (.NET 3.5) 应用程序,其中包含继承自 DataGridView 的用户控件。当用户右键单击列标题时,我想显示上下文菜单。

实际上,我在我的应用程序中的 2 个表单上找到了这个工作。我很困惑,因为 ContextMenu 不会显示在第三种形式的同一控件上。我确实看到 Popup 事件被触发,但我没有看到屏幕上绘制菜单,并且没有调用任何菜单项事件处理程序。

我完全不知道为什么我的上下文菜单没有被绘制,这让我抓狂。

不幸的是,我的代码太复杂,无法将其全部发布在这里......我包括了构建和显示菜单的简短部分。我不相信问题出在这段代码中,但我不知道问题出在哪里。

if (hti.RowIndex == -1)
{
    ClickedColumnHeader = this.Columns[hti.ColumnIndex];

    //Build a context menu and show it.
    ContextMenu mnu = new ContextMenu();
    mnu.MenuItems.Clear();
    MenuItem mnuHide = new MenuItem("Hide");
    mnuHide.Click += new EventHandler(mnuHide_Click);
    MenuItem mnuRename = new MenuItem("Rename...");
    mnuRename.Click += new EventHandler(mnuRename_Click);
    MenuItem mnuCurrencyFormat = new MenuItem("Format as Currency");
    mnuCurrencyFormat.Checked = false;

    if (this.Columns[hti.ColumnIndex].DefaultCellStyle.Format == "c")
    {
        mnuCurrencyFormat.Checked = true;
    }
    mnuCurrencyFormat.Click += new EventHandler(mnuCurrencyFormat_Click);
    MenuItem mnuSeparator = new MenuItem("-");
    MenuItem mnuShow = new MenuItem("Show");

    foreach (DataGridViewColumn col in this.Columns)
    {
        if (col.Visible == false)
        {
            MenuItem x = new MenuItem(col.HeaderText);
            x.Click += new EventHandler(x_Click);
            mnuShow.MenuItems.Add(x);
        }
    }

    mnu.MenuItems.Add(mnuHide);
    mnu.MenuItems.Add(mnuRename);
    mnu.MenuItems.Add(mnuCurrencyFormat);
    mnu.MenuItems.Add(mnuSeparator);
    mnu.MenuItems.Add(mnuShow);

    //for debugging...
    mnu.Popup += new EventHandler(mnu_Popup);
    mnu.Collapse += new EventHandler(mnu_Collapse);

    mnu.Show(this, new System.Drawing.Point(f.X, f.Y));
}

我非常感谢社区可以提供的任何帮助。我真的希望我只是在这里做了一些愚蠢的事情。

I'm writing a C# (.NET 3.5) app with a usercontrol that inherits from DataGridView. When the user right-clicks a column header, I want to display a context menu.

I've actually got this working find on 2 forms in my app. I'm stumped because the ContextMenu won't show on the same control on a third form. I do see that the Popup event gets fired, but I don't see the menu being drawn on the screen, and none of the menu item event handlers are getting called.

I have absolutely no idea why my context menu isn't being drawn, and it's driving me batty.

Unfortunately, my code is too complex to post all of it here... I'm including the short section where I build and display the menu. I'm not convinced the problem is in this code, but I don't know where else it would be.

if (hti.RowIndex == -1)
{
    ClickedColumnHeader = this.Columns[hti.ColumnIndex];

    //Build a context menu and show it.
    ContextMenu mnu = new ContextMenu();
    mnu.MenuItems.Clear();
    MenuItem mnuHide = new MenuItem("Hide");
    mnuHide.Click += new EventHandler(mnuHide_Click);
    MenuItem mnuRename = new MenuItem("Rename...");
    mnuRename.Click += new EventHandler(mnuRename_Click);
    MenuItem mnuCurrencyFormat = new MenuItem("Format as Currency");
    mnuCurrencyFormat.Checked = false;

    if (this.Columns[hti.ColumnIndex].DefaultCellStyle.Format == "c")
    {
        mnuCurrencyFormat.Checked = true;
    }
    mnuCurrencyFormat.Click += new EventHandler(mnuCurrencyFormat_Click);
    MenuItem mnuSeparator = new MenuItem("-");
    MenuItem mnuShow = new MenuItem("Show");

    foreach (DataGridViewColumn col in this.Columns)
    {
        if (col.Visible == false)
        {
            MenuItem x = new MenuItem(col.HeaderText);
            x.Click += new EventHandler(x_Click);
            mnuShow.MenuItems.Add(x);
        }
    }

    mnu.MenuItems.Add(mnuHide);
    mnu.MenuItems.Add(mnuRename);
    mnu.MenuItems.Add(mnuCurrencyFormat);
    mnu.MenuItems.Add(mnuSeparator);
    mnu.MenuItems.Add(mnuShow);

    //for debugging...
    mnu.Popup += new EventHandler(mnu_Popup);
    mnu.Collapse += new EventHandler(mnu_Collapse);

    mnu.Show(this, new System.Drawing.Point(f.X, f.Y));
}

I'd really appreciate any assistance the community could offer. I'm really hoping I'm just doing something dumb here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-08-09 20:44:55

是的,没有足够的信息来重新创建问题,但我可以提供这一点,如果您让它在其他两种形式上工作,问题不在于您继承的代码。问题在于第三种形式。

如果你真的想参加
是时候尝试从头开始制作第四个了
看看是否会出现同样的错误。查看
所有事件处理程序。听起来
就像您在表格 1 和表格 1 上连线一样2
但不是 3。

yup, not enough info to re-create the issue but may I offer this, if you have it working on the other 2 forms, the issue is not with your inherited code. The issue is with the 3rd form.

If you really feel like taking the
time try making a 4th from scratch and
see if the same error crops up. Check
all of the event handlers. It sounds
like you have one wired on forms 1 & 2
but not on 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文