-->

父窗体大家带来前方点击一个子窗体的菜单条时(Parent form is bringing to f

2019-10-17 13:37发布

我有一个很奇怪的,但reproductible问题。

我有一个MenuStrip其可以打开一个新的无模式形式与Form.Show()方法。

子窗体也有一个菜单条。

当您通过单击子窗体的菜单条开始奇怪的事情发生。 然后父窗体是回来前景和打招呼。 这是一个真正的痛苦。

如何防止这个问题?

一个史科瑟思电影通过以下链接来说明我的问题zippyshare.com (3MO)

正如你可以看到在视频,父窗体不采取重点,它只是通过其他somehing前bringed。

需要注意的是repacing的MenuStripToolStrip解决这个问题。

一些代码来重现问题:

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();
    }
}

Answer 1:

这是在.NET 4.5引入了一个非常讨厌的错误。 KB文章是可以在这里找到 。 解决方法是目前仅可作为一个修补程序,希望这将使其很快成为一个服务更新。 我只是复制/粘贴的说明:

假设你有一个基于4.5的.NET Framework Windows窗体应用程序。 当您单击菜单项来打开应用程序中的子窗口,与菜单和子窗口交互行为不正确。

例如,您可能会遇到以下情况:

当您在子窗口中打开上下文菜单,主窗口中把焦点。
不能使用助记符访问的菜单项。

出现此问题的原因IMessageFilter接口脱钩太积极。 因此,在.NET Framework 4.5不滤镜菜单中相关的窗口消息。


更新:这个问题是固定在1月8发布的.NET 4.5的更新,2013年的知识库文章是在这里 。



文章来源: Parent form is bringing to front when the menu strip of a child form is clicked