Parent form is bringing to front when the menu strip of a child form is clicked
Asked Answered
T

1

1

I have a pretty strange but reproductible problem.

I have a MenuStrip which can open a new Modeless form with the Form.Show() method.

The child form also have a Menu Strip.

The strange stuff happens when you start by clicking the menu strip of the child form. Then the parent form is coming back foreground and saying hello. That's a real pain.

How to prevent this problem?

A Scorcese movie to illustrate my problem by following this link zippyshare.com (3Mo)

As you can see in the video, parent form doesn't take focus, it is just bringed front by somehing else.

Note that repacing the MenuStrip by a ToolStrip correct the problem.

Some Code to reproduce the problem:

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();
    }
}
Tbar answered 4/1, 2013 at 15:31 Comment(7)
It appears that your form is spawning a new instance of itself. That could cause lots of problems. Better spawn an instance of something else.Arable
I don't see what kind of problem this could do. Have you an example?Tbar
No, I don't have an example. If I did, I would have written an answer, not a comment. But you have two identically named parentless forms containing identically named controls. I would suspect this to be the problem before I suspect anything else.Arable
Also, it could possibly be that the object created by (new DemoLostfocus()).Show(); gets garbage-collected immediately after it is created. That could be the source of lots of problems, too.Arable
This problem doesn't seem to happen on .Net 3.5, but it does repro on .Net 4.0.Kunming
Using new DemoLostfocus().Show(this); seems to solve the problem, but now it takes two clicks to open the child menu.Kunming
That's less annoying! Still annoying, but far more less.Tbar
S
4

This is a pretty nasty bug that was introduced in .NET 4.5. The KB article is available here. The fix is right now only available as a hotfix, hopefully it will make it into a service update soon. I'll just copy/paste the description:

Assume that you have a .NET Framework 4.5-based Windows Form application. When you click a menu item to open a child window in the application, interactions with the menu and child window behave incorrectly.

For example, you may experience the following:

When you open a context menu in the child window, the main window takes the focus.
You cannot use mnemonics to access a menu item.

This issue occurs because the IMessageFilter interface is unhooked too aggressively. Therefore, the .NET Framework 4.5 does not filter menu-related window messages.


Update: this issue was fixed in a .NET 4.5 update released on Jan 8th, 2013. The KB article is here.

Salmanazar answered 4/1, 2013 at 16:8 Comment(2)
Ok, so I lost against the framework, I will just use a ToolStrip!Tbar
Unfortunately the patch works only on Windows 8, not windows 7 so far :-/Veilleux

© 2022 - 2024 — McMap. All rights reserved.