I want to close a window in WPF after clicking a button that sends me to another window.
I tried with win3.Close(); but it doesn't work.
This is the Main Window that references to the second window.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window2 win3 = new Window2();
win3.Show();
}
Or it should be hidden?
You only need to input this.Close(); before showing next window.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Window2 win3 = new Window2();
this.Close();
win3.Show();
}
You can use this.Close(), this will close WPF window as like winform.close();
But in your case you can use this code:
Window wpfForm = Application.Current.Windows.OfType<Window>().SingleOrDefault(win => win.Name == "YourNameOfWindow");
wpfForm.Close();
you could use
Application.Current.Windows[1].Close();
Related
I am developing a little applicacion, using Visual Studio 2022, together with .NET 6.0
I have a MainWindow and attached a subordinate Window which I am showing as follows:
private void s_Betrieb_Click(object sender, RoutedEventArgs e)
{
BetriebWindow betriebWindow = new();
betriebWindow.Owner=this;
betriebWindow.Show();
}
The subordinate Window BetriebWindow is an ordinary Window, defined in XAML, whitch shows an input-Form (using textBoxes and labels).
After having changed some input, the user clicks on "Close" (the red x in the right upper edge of the Window). I am asking the user wheather he would like to save his changes or not, using a MessageBox.Show().
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Wollen Sie Ihre ",...) == MessageBoxResult.OK)
SaveData();
Close();
}
After the subordinate Window has closed, the MainWindow is minimizing itself and appears in the taskbar, which I (and perhaps even the future user) don't like.
What am I doing wrong? I want the MainWindow to remain opened on the screen.
But closing the MainWindow should close the subordinate window, too.
Thanks for an answer!
Paul
Remove the call to Close() in the Closing handler and call Activate() on the main window when the child window has been closed:
private void s_Betrieb_Click(object sender, RoutedEventArgs e)
{
BetriebWindow betriebWindow = new();
betriebWindow.Owner = this;
betriebWindow.Closed += (ss, ee) => Activate();
betriebWindow.Show();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Wollen Sie Ihre ", ...) == MessageBoxResult.OK)
SaveData();
}
I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);
How to set the first window to be read only until I close the second one?
In button click i open new Window
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2 = new window2();
win2.Show();
}
Now the first window should be read-only.
After closing the second window, the first one should have full access again
My app is multi-window, here is quickly how it works:
In main window I have a list of items, when I click on one, it opens another window where I can modify it. When I close that window, I want main window to refresh its content. I've tried many event handlers including GotFocus() but it doesn't want to launch my method to refresh the list. Any advise?
If you want something to happen when the other window is closed, you can subscribe to its closed event. This will fire when the windows is closed.
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window1();
wnd.Closed += wnd_Closed;
wnd.Show();
}
void wnd_Closed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
}
I have two windows with WindowStyleset set a none, MainWindow and window1. I'd like to open window1 and close it when I click on a close button. Window1 is only used for configuration values.
I've tried with: In MainWindow I have a menu
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Window1 secondWindow = new Window1();
secondWindow.ShowDialog();
}
In Window1 close button code
private void btnQuit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
When I click on close button, the app is closed. If I change ShowDialog() for Show() the app is closed. How can I do it? I only want to close window1 and stay in MainWindow?
Thank you!.
it's related to which of the Windows that you have is the Main window of the application
try to set App.Current.MainWindow to your Main window like this
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
App.Current.MainWindow = this;
Window1 secondWindow = new Window1();
secondWindow.ShowDialog();
}