When child window is shown, parent window is minimized - c#

Is it possible to make that when child window is opened, parent window is minimized and is inactive (child window is modal)?
I tried changing WindowState property to minimized for parent window before calling child window, but then child window starts minimized.

Not sure why you want to do it this way, but this is doable with some trickery (or good design patterns). With trickery you can do this:
From parent (form1):
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
new Form2().ShowDialog(this);
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
Although, I would recommend reconsidering your architecture instead.

There is a flaw in your design.
You have two top-level Windows, the main form and the child modal dialog. The child modal dialog is what is known as, in Win32 terminology, an owned window. The main form is un-owned. When a top-level un-owned Window is minimized, all the windows that it owns are hidden. That is functionality provided by the desktop window manager.
The documentation states:
An owned window is hidden when its
owner is minimized.
What you are trying to do sounds unusual anyway. Normally when a form shows a modal dialog, the modal dialog is shown on top of the other form. Why are you wanting to hide the main form?
If you are dead-set on this design you need to arrange that your modal dialog is an un-owned window. When you do so it will appear as an item in the taskbar separate from your main form. Is this what you want?

In the child form_Load event, try this :
frmParent frm = new frmParent();
frm.WindowState = FormWindowState.Minimized;

Related

C# wpf multiple windows on task bar

I created a C# project which have multiple windows. When I do some other stuff, some of the windows are lost focus. Then I have to bring them to front by clicking the task bar one by one.
I am wondering is there are OnTaskBarClick Event on C# wpf window?
Then What I need to do is to set all sub windows with "ShowInTaskbar="False"". Then when ever my mainwindow is clicked on the task bar, I bring all the windows to the front.
You'll want to use the Window.Activated event to detect when your application's window is brought into focus:
From the documentation:
A window is activated (becomes the foreground window) when:
The window is first opened.
A user switches to a window by selecting it with the mouse, pressing ALT+TAB, or from Task Manager.
A user clicks the window's taskbar button.
Or you could use the Application.Activated event.
Clicking on a control of an already active window can also cause the Window.Activated event to fire, so if the issue is that it's firing too often, you'll probably want to watch for when the application toggles between being active and deactive.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Application.Current.Activated += CurrentOnActivated;
Application.Current.Deactivated += CurrentOnDeactivated;
}
private bool isDeactivated = true;
private void CurrentOnDeactivated(object sender, EventArgs eventArgs)
{
isDeactivated = true;
//handle case where another app gets focus
}
private void CurrentOnActivated(object sender, EventArgs eventArgs)
{
if (isDeactivated)
{
//Ok, this app was in the background but now is in the foreground,
isDeactivated = false;
//TODO: bring windows to forefont
MessageBox.Show("activated");
}
}
}
I think what you are experiencing is the window Owner not being set on your child window.
Please refer to Window.Owner Property
When a child window is opened by a parent window by calling
ShowDialog, an implicit relationship is established between both
parent and child window. This relationship enforces certain behaviors,
including with respect to minimizing, maximizing, and restoring.
-
When a child window is created by a parent window by calling Show,
however, the child window does not have a relationship with the parent
window. This means that:
The child window does not have a reference to the parent window.
The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be
minimized, maximized, and restored independently of the other.
You can easily fix this by setting the Owner property in the child window when before calling Show() or ShowDialog()
Window ownedWindow = new Window();
ownedWindow.Owner = this; // this being the main or parent window
ownedWindow.Show();
Note : if conforming to an MVVM pattern, this can become a little
more cumbersome, however there is plenty of resources on how to link owner and parent windows.

Minimizing a WPF window owned by a Winform is not working as expected

I have a Form control named MyForm, which is the parent of some WPF BaseWindow, named MyWindow.
I'm setting their relationship as follows:
new WindowInteropHelper(myWindowInstance).Owner = myFormInstance.Handle;
And am showing the window using
myWindowInstance.ShowDialog();
The window is set with
ResizeMode="CanResize"
Therefor, it has a minimize button. On minimizing the window, its not minimized as expected, but rather minimized to the bottom of the form.
What I would like to experience is that the parent would be minimized as well. Meaning that minimizing the window, will be translated to minimizing the form.
You probably want to create event handlers to control their behavior relationship when events (minimize) occurred.
For WPF, you could use Window_StateChanged event with checking if this.WindowState == WindowState.Minimized
Then for the WinForm you could do the trick by Resize event and checking if WindowState == FormWindowState.Minimized
If any of these is true, then you could minimize both.

WinForms Form doesn't show after activation

I have a WinForms Form I call from two places, one from a tray-icon's context menu and from a context menu I get from right-clicking on my app's shortcut on the desktop. When I use the tray-icon's menu the form opens just fine, even if it was minimized - it displays again. The problem is when I use the link. If the form is minimized, sometimes it will restore it, but other times it won't. The application's icon on the task-bar will flash but the window itself won't show.
I checked through debugging and both calls to display the form are from the same thread. Are there other contexts I'm missing that might interfere?
Code I use:
public void Display()
{
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
this.Focus();
}

winforms MDI how to make a child form look like a natural part of the parent

The problem is this - I have a main form (parent) with a menu and several other forms which are children I want them to load in the main form. The problem that I met is - the child form is loaded within the main form(parent) but it looks like a separate window. This:
And it not only stays like window within a window but also don't get resized when the parent is which seems logical but is not what I want.
What I want is something like this :
I want the child forms to be loaded without the window controllers for minimize, maximize, close, the blue line and all that stuff but instead to look like a part of the parent window and respond to some parent events like resize.
Try adding the following code into your children windows (and subscribe on FormLoaded event)
private void FormLoad(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}

ShowDialog() doesn't make the window modal

I have a windows form that pops up a dialog box if certian conditions are met when the form loads. The problem is the window does not stay on top and I can still click thing on the parent. However, there is a button on the form that when pressed opens the same window, when I do this it works as expected (like a dialog window).
Is there an issue with showing a dialog when a form is first loading?
Are you calling ShowDialog from the Form class? Because it will only set the parent window if called from another Form. Alternatively you can use the overload that has the IWin32Window parameter to specifically set the owner.
can you explain the issue further as this is my code which do not show the form it self until the dialog has been closed either you set the parent or not
private void Form1_Load(object sender, EventArgs e)
{
//your functionality goes here
AboutBox1 box = new AboutBox1();
box.ShowDialog();
}
}
on the other side you can also check with TopMost property
The ShowDialog method needs to be called from the form that you want to be it's parent/owner in order for it to be modal to that form. Alternatively I believe you can set the owner of a dialog directly but I have never needed to do that.
DaBomb,
To do what you want, you will have to call your modal dialog from the constructor of your main form, NOT from the Form_Load event.
Something like this:
public Form1()
{
InitializeComponent();
this.Show();
Form2 popupForm = new Form2();
popupForm.ShowDialog();
}

Categories

Resources