Maximize MDIchild to MDIParent automatically on form load - c#

I am new to WinForms and working on a parent child application. I have created a MDI parent and loading a child form inside it.
Once I select the menu in MDI parent, the child form gets loaded but not fully maximized. I have to manually maximize it to fit the MDI Parent. Below is the screenshot of what I am getting during form load.
Every time on form load i have to maximize it. Below is the code that I am using.
private void newDeploymentToolStripMenuItem_Click(object sender, EventArgs e)
{
NewDeployment nwDeploy = new NewDeployment();
nwDeploy.MdiParent = this;
nwDeploy.Dock = DockStyle.Fill;
nwDeploy.WindowState = FormWindowState.Maximized;
nwDeploy.Show();
}
I want the child form to be loaded in maximised state as shown below. I have searched google but I am getting the same code that I have used.
Is there any other way of doing this? Any help will be highly appreciated.

Thanks to Jimi and Sinatr, I have modified the code by loading the form then modify its window state to maximized.
Below is my updated code
NewDeployment nwDeploy = new NewDeployment();
nwDeploy.MdiParent = this;
nwDeploy.Dock = DockStyle.Fill;
nwDeploy.Show();
nwDeploy.WindowState = FormWindowState.Maximized;

The best way to do this is to set the child form's WindowState to Maximized from within its own Load event. Works every time.

Related

C# Windows Form start up always minimized

I have developed a C# Windows Form.
At first, the Windows Form works fine.
However, one day the Windows Form starts up always minimized and I have no idea.
I checked the WindowState is Normal not Minimized.
How Can I fix it, Thanks!
Edit:
I comment each code block to narrow the scope to locate the problem point.
And I found that I used a Drive Detector in my MainForm.
When that instance was created, the call Window form must be passed as a parameter to the constructor.
Otherwise, the Drive Detector will create a hidden form. However, the MainForm will be minimized.
The below code will NOT create a hidden form.
driveDetector = new DriveDetector(this);
The below code will create a hidden form, it will interfere the call Windows Form.
driveDetector = new DriveDetector();
try to add this code in form load event and test
this.WindowState = FormWindowState.Normal;
you should use WindowState = FormWindowState.Maximized if you wish to open your windows in full screen by default. You can do this programmatically in Form load event.
There are other various options available too from which you can control on How to open your windows form.
1.Check whether you have set the size of form to smaller one.
2.Try re-build your solution.
3.Add Form Load Event from your Events Properties of Form and add following code to it
this.WindowState = FormWindowState.Normal;
Try to do it in form activated event
bool bIsLoaded = false;
private void Form1_Activated(object sender, EventArgs e)
{
if (!bIsLoaded)
{
this.WindowState = FormWindowState.Maximized;
bIsLoaded = true;
}
}
Just try to add it from the code level to say the windows state as follows.
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
I comment each code block to narrow the scope to locate the problem point.
And I found that I used a Drive Detector in my MainForm.
When that instance was created, the call Window form must be passed as a parameter to the constructor.
Otherwise, the Drive Detector will create a hidden form. However, the MainForm will be minimized.
The below code will NOT create a hidden form.
driveDetector = new DriveDetector(this);
The below code will create a hidden form, it will interfere the call Windows Form.
driveDetector = new DriveDetector();
Try this:
Topmost = true;
In your Form_Load event

MDI child form in Windows Forms

I have a form with an MDI container marked as true. This form is a parent form which has a child form. In the child form I use this code for loading it in maximized size.
this.WindowState = FormWindowState.Maximized;
I use this codes for showing the child form :
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show();
And also child form is fixed single so that it won't be able to resize. But however i disable maximize button it will disapper and can be restore down.
Try this:
f2.MinimizeBox = false;
You can try
f2.ControlBox = False;
That could hide both the MaximumBox and MinimumBox but not the Close Button.
Had the same problem, what ended up doing the trick was handling the OnMove event and resetting the visibilities there (even though they are already set to false). Not sure why that works, but it did for me.

Why I got extra close button on mdi child window?

I got a strange problem. My mdi child form has 2 close buttons and 2 maximized buttons.
A screenshot of the problem:
I create the mdi child like this:
summaryForm.MdiParent = ContainerForm;
summaryForm.WindowState = FormWindowState.Maximized;
summaryForm.Show();
If I get rid of "summaryForm.WindowState = FormWindowState.Maximized;", the window style is correct. But I hope to make the mdi child form maximized when created.
It's a bug in Winforms. This will happen when the child is created by the parent's constructor. Move it to the Load event.
try this:
childform.ControlBox = false;

Child Form is hidden behind MDI Parent Container

When a child form is opened it is hidden behind the title bar of MDI Parent Container.
The Child form's WindowState is set to Maximized. FormBorderStyle is set to None.
If I minimize the MDI parent and maximize it, then the child form comes in to front.
How to overcome this situation?
Edit:
I use the following code to open a child form.
this.childForm= new ChildForm();
this.childForm.MdiParent = this;
this.WindowState = FormWindowState.Maximized;
this.childForm.Dock = DockStyle.Fill;
this.childForm.Show();
this.childForm.BringToFront();
this.childForm.Focus();
Try the following code.
Form1 newMDIChild = new Form1();
newMDIChild.MdiParent = this;
newMDIChild.Show();
this.LayoutMdi(MdiLayout.Cascade);
newMDIChild.Dock = DockStyle.Fill;
The native Windows MDI implementation cannot deal with borderless MDI child windows. Unfortunately, Winforms forgets to enforce that restriction. You can move the WindowState assignment after the Show() call but that causes another problem.
Just don't make it borderless, the border isn't visible anyway.
AboutBox1 ab = new AboutBox1();
ab.MdiParent = MDIForm.ActiveForm;
ab.TopMost = true;
ab.Show();

Show MDI child Always on top of other MDI child

How do I show a MDIChild Form always on top of other MDIChild Forms ?
I have set TopMost property of the ChildForm to True, But the form still behaves the same way...
I have tried to set TopLevel property of ChildForm to True and got the error message... "Top-level Style of a Parented control cannot be changed."
How do I achieve this.
Thanks
A better solution that doesn't requiring changing every other form: - declare the new toolbox as a control of the Main Parent (this):
fForm fFormObj = new fForm();
fFormObj.TopLevel = false;
this.Controls.Add(fFormObj);
fFormObj.Parent = this;
fFormObj.TopMost = true;
fFormObj.Show();
The framework apparently does not support MDI child windows owning each other so you have to simulate that behavior yourself:
static Form f1 = new Form();
static Form f2 = new Form();
static Form f3 = new Form();
[STAThread]
static void Main()
{
f1.IsMdiContainer = true;
f2.MdiParent = f1;
f3.MdiParent = f1;
f1.Show();
f2.Show();
f3.Show();
f2.Activated += new EventHandler(f2_Activated);
Application.Run(f1);
}
static void f2_Activated(object sender, EventArgs e)
{
f3.Activate();
}
I generally just make owned forms not be MDI child forms. They don't stay in the MDI container, but at least they stay in front.
Perhaps the reason this limitation exists is because of the strange or ambiguous desired behavior when the MDI child that is the owner is maximized within the container. the above code will allow the owned form to go behind the maximized parent if you click on it in this case. If you have it outside the container, though, then it will remain visible.
//Edit
Since only one of your MdiChild form needs to be focused, try the following:
In the MdiChildActivate event re-focus or re-activate the required window as the activated child window.
You could also use the Deactivated event to enforce the re-focusing of the concerned child window.
When you create the form and show it also append a call to focus method.
ChildForm.Focus()
Setting the focus should make it topmost.
Hope it helps.

Categories