我有一个非常奇怪但可重现的问题。
我有MenuStrip
可以使用Form.Show()
方法打开新的无模式表单。
子表格也有菜单条。
当您通过单击子表单的菜单条开始时,会发生奇怪的事情。然后父表格回到前台并打招呼。这真是一种痛苦。
如何预防此问题?
Scorcese电影,通过以下链接说明我的问题zippyshare.com(3Mo)
正如您在视频中看到的那样,父表单不会引起注意,它只是通过其他方式在前面构建。
请注意,通过MenuStrip
对ToolStrip
进行重新校正可以解决问题。
重现问题的一些代码:
public class DemoLostfocus : Form
{
private void InitializeComponent()
{
this.menuStrip1 = new MenuStrip();
this.fileToolStripMenuItem = new ToolStripMenuItem();
this.openModelessFormToolStripMenuItem = new ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
this.menuStrip1.Items.AddRange(new ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(284, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
this.fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
this.openModelessFormToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "File";
this.openModelessFormToolStripMenuItem.Name = "openModelessFormToolStripMenuItem";
this.openModelessFormToolStripMenuItem.Size = new System.Drawing.Size(187, 22);
this.openModelessFormToolStripMenuItem.Text = "Open Modeless Form";
this.openModelessFormToolStripMenuItem.Click += new System.EventHandler(this.openModelessFormToolStripMenuItem_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "DemoLostfocus";
this.Text = "DemoLostfocus";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private MenuStrip menuStrip1;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem openModelessFormToolStripMenuItem;
public DemoLostfocus()
{
InitializeComponent();
}
private void openModelessFormToolStripMenuItem_Click(object sender, EventArgs e)
{
(new DemoLostfocus()).Show();
}
}
答案 0 :(得分:4)
这是.NET 4.5中引入的一个非常讨厌的错误。知识库文章是available here。修复程序现在只作为修补程序提供,希望它很快就会成为服务更新。我只是复制/粘贴说明:
假设您有一个基于.NET Framework 4.5的Windows窗体应用程序。单击菜单项以在应用程序中打开子窗口时,与菜单和子窗口的交互行为不正确。
例如,您可能会遇到以下情况:
在子窗口中打开上下文菜单时,主窗口将获得焦点 您不能使用助记符来访问菜单项。
出现此问题是因为IMessageFilter接口过于疏忽。因此,.NET Framework 4.5不会过滤与菜单相关的窗口消息。
更新:此问题已在2013年1月8日发布的.NET 4.5更新中修复。知识库文章is here。