views:

285

answers:

3

I am writing an application where I have a form with a panel. I have noticed that when I add another form to the panel, that the added form's keyboard shortcuts stop working.

I am using the following code :

MainMenu m = new MainMenu();
m.TopLevel = false;
m.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
m.Dock = System.Windows.Forms.DockStyle.Fill;
pnl.Controls.Add(m);
m.Visible = true;
pnl.ResumeLayout();

Is there anyway to make the keyboard shortcuts work?

Regards

+1  A: 

My first guess (and it's totally a guess) is that you need to pass the parent/owner when constructing the child object, rather than just assigning parent ... could you show us that part of your code?

Also, just glancing over your code, it seems strange to, for a MainMenu, set Dock to Fill....

overslacked
A: 

Adding a form to a panel inside another form? That is not a supported scenario; frankly I'm surprised it doesn't throw an exception. A better way to do this would be to use UserControls.

It works fine. I have used this many times in the past. The only thing is that the keyboard shortcuts do not appear to work
Ace Grace
How do you know it isn't a supported scenario?
Ace Grace
I will admit that I can't find any documentation explicitly stating one way or the other. But based on what I know about Windows Forms, this is not supported. Just because you can make it work under certain circumstances does not mean it is supported, or that you should expect all functionality to work correctly.
@commongenius - I've used this ... "pattern" ... in a couple of different programs. The first was an old C++ program, and as long as you do the reparenting correctly, a window handle is just a window handle, and everything gets moved over without a problem. It's a bit trickier in .Net because you're so far removed from the API and like you indicate, I'm sure this is not anything they were intending ... however, if you make sure you get the assignments right, it's just window handles, and it works fine.
overslacked
A: 

I found out that it was due to focus issues.

I have since converted my forms to user controls and the problems have gone away.

Ace Grace