Reputation: 5750
In a container form I have menu and buttons to open ther forms.
Here I am facing a problem when I open any form these buttns and lables come over newly opened form.
Please guide me how I can manage this issue? I want to open a new form and keep these container form's controls in back ground of it.
Upvotes: 4
Views: 12024
Reputation: 942119
I think I see what you did. You are using MDI and you put the menu labels and buttons on the MDI parent form. You did something with the MDI client window, it is normally dark-gray. Maybe you figured out how to change its BackColor or changed the Windows system color.
Yes, your screenshot is then the expected result. The problem is that MDI client forms are parented to the MDI client window. Which makes them show up behind the controls you put on the parent form.
There is no workaround for this, you are going to have to change your UI. To keep MDI, put a Panel on the parent form and set its Dock property to Left. Move the menu controls on that. The MDI client window will now shrink, occupying the remainder of the parent form. And the child forms will constrain themselves to that area. The wee painful bit is that you'll have to reorganize the menu to fit the much smaller space available in the panel.
Upvotes: 6
Reputation: 1
I had the same issue and I have found the best solution for it. First of all you need to shift your controls in a panel. The add a "MdiChildActivate" event and write this,
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
if (ActiveMdiChild != null)
panel1.SendToBack();
else
panel1.BringToFront();
}
Upvotes: 0
Reputation: 11
Its Very Simple
Create new form (frm_chiled_mdi) after mdi form, and decorate it as you wish (like button, picture, label etc.)
Load it on MDI Form Load
MDI Form Load Coding..
Dim frm As New frm_chiled_mdi
frm.MdiParent = Me
frm.Show()
form load coding of frm_chiled_mdi
Me.WindowState = FormWindowState.Maximized
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.MaximizeBox = False
Me.MinimizeBox = False
Try
Me.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo.jpg")
Catch ex As Exception
End Try
Upvotes: 1
Reputation: 25
I had this problem and solved it this way:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
Form2 F2;
public Form1()
{
InitializeComponent();
F2 = new Form2();
}
private void Form1_Load(object sender, EventArgs e)
{
Panel P1 = new Panel();
P1.Location = new Point(0, 0);
P1.Height = this.Height;
P1.Width = this.Width;
P1.BackColor = Color.Transparent;
this.Controls.Add(P1);
SetParent(F2.Handle, P1.Handle);
F2.Owner = this;
F2.Show();
}
}
Upvotes: 0
Reputation: 171
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
Upvotes: 2
Reputation: 66
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;
}
}
Upvotes: 5
Reputation: 6711
@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.
Upvotes: 1
Reputation: 54552
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
Upvotes: 4
Reputation: 51369
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();
Upvotes: 0
Reputation: 38820
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);
Upvotes: 0