In a container form I have menu and buttons to open ther forms.
Here I am fac
It appears as though that form is a sibling of those other child controls. Do you have to open it as a child of that window? Can't it be like a non-modal dialog box and not a child window of that main form?
If it has to be within that main form and a sibling of those controls, then you're going to have to set the Z-Order of it. There's no property for that, so you're going to have to look toward the Win32 API call, SetWindowPos
:
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(
int hWnd, // window handle
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags); // window positioning flags
const uint SWP_NOSIZE = 0x1;
const uint SWP_NOMOVE = 0x2;
const uint SWP_SHOWWINDOW = 0x40;
const uint SWP_NOACTIVATE = 0x10;
And call it something like this:
SetWindowPos((int)form.Handle, // that form
(int)insertAfter.Handle, // some other control
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
@Hans Passant has the correct answer, but you could also solve your issue by not using MDI forms at all. Some options:
Parent
property to the menu form, orBoth of these would require significant changes to your UI code, though.
I've also got the same problem. I got an alternative solution as described below:
And did the following
private void timer1_Tick(object sender, EventArgs e)
{
if ((int)MdiChildren.GetLength(0) > 0)
{
panel1.Visible = false;
}
else
{
panel1.Visible = true;
}
}
If it is a MDI application and you put controls in the parent window then they will be shown on top of any created child windows. You need to have your menu in a child window also, not on the parent form.
Look at this Article and this.
expecially this:
The parent Form may not contain any controls. >
Edit: Added Additional Information
Call BringToFront() on each child form after showing them. Alternately, hook it to each child form's OnLoad method:
childForm.OnLoad += (s, e) => (s as Form).BringToFront();
The main trick here is to treat child forms as Controls. You'll create a child form just like any other control. When you use this method, you have to set it's TopLevel to false - otherwise it won't work.
The following lines of code are used to create a child form:
Form childForm = new Form(); //initialize a child form
childForm.TopLevel = false; //set it's TopLevel to false
Controls.Add(childForm); //and add it to the parent Form
childForm.Show(); //finally display it
childForm.BringToFront(); //use this it there are Controls over your form.
more details here