WPF MainWindow doesn't close with .Close - c#

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;
}

Related

How to close Window in WinUI3?

I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.

"Swapping" Windows in C# WPF instead of opening a new one?

I'm trying to create a "Guide" app for a videogame. I want a button to clear everything in the window and show something else instead of creating another window. I've been using the code:
Window1 w1 = new Window1();
w1.Show();
this.Close();
Instead of this method, which closes the current window and opens a new one, is there a way to clear everything in the window and pop up other information?
You could create a Frame in your MainWindow and display different pages.
Xaml Frame:
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden" />
After that you create a new Page-Control and navigate to it
var page = new CustomerPage();
MainFrame.Navigate(page);

WPF Child window not opening windowstate Normal

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.

Wpf minimizes Window?

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?

How do I focus a modal WPF Window when the main application window is clicked

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;

Categories

Resources