I have a main Window.Then if you touch settings button a new modal Window is shown with some options..Then you click an option and a new Window (lets call it "Settings") is shown.Both windows have as owner the main Window. Our new window(Settings) may open new window having itself as owner for example if you want to add a new staff member.And here is the problem. When you close the new window and then close Settings window the main Window is minimized... However this doesnt happen if Settings window dont open any other window...
this is how i show the forms
UserForm f = new UserForm();
f.Owner = this;
f.Show();
Use MainWindow.Activate() on close of Child Window.
it should work
I ran into the same problem and found an apt solution here.
Set the window owner to null before the Settings window closes.
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Owner = null;
}
Can't you catch when the window is closed (OnFormClose I think) and then unminimise your main window?
Related
I'm working on a small project where I have a login window and when the user is autheticated it will open a new window, that works but after the user logged in I want to close the Login window how ever, the main window doesn't close.
MainWindow mainWindow = new MainWindow();
mainWindow.Close();
Loader loader = new Loader();
loader.Show();
You are initializing a new instance of a Window and close it immediateley afterwards.
It looks like your mainWindowis not the login window you want to close. If you want to close the current Main Window of you application, you can use:
App.Current.MainWindow.Close();
Make sure you also set a new Main Window. I assume you wanted to do something similar to this, also assuming "loader" is your new Window:
private void Login(){
App.Current.MainWindow.Close();
Loader loader = new Loader();
App.Current.MainWindow = loader;
}
I have the following simple wpf application:
App.xaml:
<Application x:Class="TestWpf2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>
App.xaml.cs:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var parentWindow = new Window();
parentWindow.Show();
var childWindow1 = new Window { Owner = parentWindow };
childWindow1.Show();
var childWindow2 = new Window { Owner = parentWindow };
childWindow2.Show();
}
}
The application causes 3 windows to appear on screen. If you run the application and close the two child windows, the parent window is minimized to the task bar. If you comment out childWindow2.show(), run the application, and close the single child window, the parent window is not minimized to the taskbar.
I can add the following code to work around this problem:
childWindow1.Closing += delegate(object sender, CancelEventArgs ex)
{
(sender as Window).Owner = null;
};
but I don't want to use a hack like this, and I want to understand why this problem occurs.
Why does this happen?
This is a WPF undocumented feature(Bug)
This bug has been reported to Microsoft more than 7 years ago.
Modal dialog on top of non-modal window sends main window to back.
The WPF team has recently reviewed this issue and will not be
addressing this issue as at this time the team is focusing on the bugs
impacting the highest number of WPF developers.
Let's look at the behavior of this undocumented feature a bit.
It doesn't Minimize, just go one window behind (in debug mode, behind the visual studio window)
Steps to prove:
Run this application.
Minimize all other windows (eg: visual studio and all other windows). Keep only these three windows on the screen.
Close both children, parent is still in Normal State
Test this code for further proof:
StateChange wouldn't fire.
protected override void OnStartup(StartupEventArgs e)
{
var parentWindow = new Window() { Title = "Parent" };
parentWindow.StateChanged += (sender, ep)=>
{
var state = ((Window)sender).WindowState;
};
parentWindow.Show();
var childWindow1 = new Window { Owner = parentWindow, Title = "Child1" };
childWindow1.Show();
var childWindow2 = new Window { Owner = parentWindow, Title = "Child2" };
childWindow2.Show();
}
Being TopMost prevent this happening
If parent window is TopMost, this wouldn't happen. But this may not be an option in many cases.
parentWindow.Topmost = true;
Workaround
Activate the parent on child2 close.
childWindow2.Closed += (a, b) => { parentWindow.Activate(); };
This is due to the fact that you can't use the parent window once the child window is shown with the Owner set. Just try to access the parent window while the child is on the screen, and it won't let you, to see what i mean.
If you don't specify Owner then this behaviour doesn't happen.
I never worked out what is causing this to happen! In the end, I wrote this code that is called after any child window is closed:
// App inherits from Application, and has a Window property called MainWindow
// and a List<Window> property called OpenWindows.
if (!App.OpenWindows.Any())
App.ParentWindow.Activate();
So if the last child window is closed (making App.OpenWindows.Any() false) the parent window is activated. This does not result in flicker (the main window minimizing then maximizing, for instance.)
This isn't the best solution. I will not close this question in the hope that someone can explain why WPF has this functionality!
I have a WPF program that I want to be minimized most of the time to the system tray..
However when it is minimized to the system tray, the child Windows that open when a message is received does not open in normal window state but opens minimized in the system tray..
I have tried
msgWindow.WindowState = System.Windows.WindowState.Normal;
msgWindow.Show();
But this does not work.. I can fix it by doing
this.WindowState = System.Windows.WindowState.Normal;
msgWindow.Show();
However I do not want the main window to be opened every time a message pops up..
Please try this it will help you.
Window2 win2 = new Window2();//here i established the child window named as Window2
win2.Show();
The above code diplay both window(main and child) concurrently.
Window2 win2 = new Window2();//here i established the child window named as Window2
win2.Show();
this.Close();
The above code diplay child window only.
I'm creating a wpf application in c#, I know to close/open a window you have to use the .Close() and .Show() methods but for some reason the home screen, the first window that appears when I launch the application, won't close.
Home window1 = new Home();
window1.Close();
Name window2 = new Name();
window2.Show();
Window2 appears, but window1 won't close. What's the problem.
Where is your code for showing window1? If you show your home window somewhere else in your code, you need to use that reference in order to close it. Making a new Home object and calling its Close method will not close a window shown using another Home object.
Presumably because if you close the window you'll close the application.
If you just want to hide the main window use the window.Hide() method.
This from the help on Window.Close:
A Window can be closed using one of
several, well-known, system-provided
mechanisms located in its title bar,
including:
ALT+F4.
System menu | Close.
Close button.
A Window can also be closed using one
of several well-known mechanisms
within the client area that are
provided by developers, including:
File | Exit on a main window.
File | Close or a Close button on a
child window.
UPDATE
Tormod Fjeldskår has a good point in his answer. I assumed that the code was given as an example rather than being what was actually being used.
This is a bug in WPF. Window.Close will fail silently if the SourceInitialized event has not yet occurred. Subsequent calls to Window.Close will also fail.
https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=299100
For a workaround, add this to your Window:
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// check if we've already been closed
if (m_bClosed)
{
// close the window now
Close();
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
// make sure close wasn't cancelled
if (!e.Cancel)
{
// mark window as closed
m_bClosed = true;
// if our source isn't initialized yet, Close won't actually work,
// so we cancel this close and rely on SourceInitialized to close
// the window
if (new WindowInteropHelper(this).Handle == IntPtr.Zero)
e.Cancel = true;
}
}
bool m_bClosed;
Or you could have Window2 be the main window (you can change this in app.xaml in the StartUpUri property) and either have Window2 show and close Window1 or not show Window1 at all.
<Application x:Class="Invitrogen.TheGadget.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window2.xaml">
</Application>
I have my MainApplication Window that launches a new Window with .ShowDialog() so that it is modal.
UploadWindow uploadWindow = new UploadWindow();
uploadWindow.ShowDialog();
Now users are often leaving this window open and it can get lost under other windows. When the MainApplication is clicked you get an error-like beep and are unable to interact with it, so the modal window is blocking properly as expected, but it would be nice if the modal window was focused at this point to show the user it was still open.
Currently it just looks as if the MainApplication window has locked up.
Try setting the dialog's owner:
var uploadWindow = new UploadWindow();
uploadWindow.Owner = this;
uploadWindow.ShowDialog();
I have the problem, that I can't use this, if someone have the same problem, you can use
Window.GetWindow(this)
Since I am using MVVM, I'm not creating the code from the GUI. I used this.
var uploadWindow = new UploadWindow();
uploadWindow.Owner = Application.Current.MainWindow;
uploadWindow.ShowDialog();
If all of the above solutions tried and still facing the same problem
then here is your tested and verified solution
go to your window xaml and add
ResizeMode = "NoResize"
Nowadays you can just set Topmost = true
var uploadWindow = new UploadWindow();
uploadWindow.Topmost = true;