ContextMenu 不显示,我不知道为什么
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,没有足够的信息来重新创建问题,但我可以提供这一点,如果您让它在其他两种形式上工作,问题不在于您继承的代码。问题在于第三种形式。
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.