child form minimize goes to task bar - c#

I have an MDI form which displays a treeView control, when the user clicks on the tree node child form, it opens:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
frmPartMaster frm = new frmPartMaster();
frm.Show();
}
Here frm is displaying the backside of the tree control but I want it to show form in front of parent, not back. So I changed the code to:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
frmPartMaster frm = new frmPartMaster();
frm.Show(this);
}
Here the child form is displayed in front of the tree, but when I minimize the child form it goes to task bar.How do I get it to go to the left corner of of parent form?

You have to set the parent form's IsMdiContainer property to true. Then while opening the child form add the following code:
frm.MdiParent = this;

Are you using MDI? Then it won't come to Task bar. You can set the ShowInTaskbar option to false, which will not display for in Taskbar.

Real MDI child forms do not minimize to the taskbar. Therefore I can only conclude that you are not using an MDI child form and that your solution is to start doing so.

Related

Maximize MDIchild to MDIParent automatically on form load

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.

How to send a WPF window behind a windows form when clickwd on windows form

I am using an MDI Form for my project. It has a Form (WinForm) and a Window (WPF Window) as Children. I opened the Window and then a the Form. The Window always stays above the Form, even though I can click on the Form. The Form never comes to the front.
I tried with :
private void Window_Activated(object sender, EventArgs e)
{
this.Topmost = false;
}
// this method does not satisfy my condition.

enable my parent main window(parent window) after closing my chlid window

I want enable my parent window on closing of child window. When i use enable property it doesn't work for me. form parent to child it works, my parent window disabled.
Try this:
In parent Window:
childWindow.Closed += ChildWindowClosed;
...
private void ChildWindowClosed(object sender, EventArgs e)
{
IsEnabled = true;
}
Now, when the child Window closes, the parent Window.IsEnabled property will be set to true.
However, you probably shouldn't do this anyway... there could be negative consequences to disabling the main Window. If you just want to temporarily 'lock' the parent Window while the child Window is open, then all you have to do is to open the child Window as a dialog, like this instead:
childWindow.ShowDialog();

Order Break Between Model And Main Forms c#

I have two forms form1 is main form and form two is model form I want to set the forms as below:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this);
}
The above would set the form1 owner of form2 and form2 would be shown but the problem is that this will break the order of forms on press of Alt+Tab keys hence I have tried it with another way as below.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
This would be works but the problem is that the dialogue forms will not allow me to maximise/minimise and close
My form2 is borderless form and it is set to show on specific location as to fit with main form1. My aim to do not shows the form2 in Alt+Tab list and as I close the form2 then form1 will show immediately without break order of form.
When I press Alt+Tab keys on first condition and try to close form2 then the other application shown instead of form1 which is I do not want.
Is there any solution of this problem?.
It really sounds like you could do the second form as a custom control.
See Microsoft's documentation and this set of examples.
Think of it as a standard control, like a Button, DataGridView, TextBox, or the like, except that you have total control over it. You can show or hide it, you don't have to worry about where it is positioned, it won't take focus away from the parent form, and so on. And you can put whatever other controls you want in it, encapsulate all their logic, etc.
A possible hack is to keep your parent form active after opening child form as a modal form, so that you could do maximize/minimize your parent as well. An extension method:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
public static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
formToBeShown.ShowDialog(parent);
return formToBeShown.DialogResult;
}
You can call:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//additionally do f2.ShowInTaskbar = false to make sense.
f2.ShowDialogSpecial(this);
}
This wont let child form truly act as non-modal form, since child form can cover over parent form.

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