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();
}
Related
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);
I want to be able to press a button and have the program open up a new window and close the old one.
I have followed solutions from this link but i have never has success with any of them How do I open a second window from the first window in WPF?
Here is my work soo far:
Window editor = new Window();
editor.Show();
this.Close();
But this does nothing.
The program should open up a new window and close the old one.
The functionality you described will work just fine. The Problem there is would more likely be the function or Methode in which you call this function.
To write a Methode that would handle a Button press as you want is pretty good described here: https://www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release.
Hopefully, this will help you otherwise just ask
here is a small Implementation if that helps:
public partial class MainWindow : Window
{
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
Window editor = new MainWindow();
editor.Show();
this.Close();
}
private void MainWindow_KeyUP(object sender, KeyEventArgs e)
{
}
public MainWindow()
{
this.KeyDown += MainWindow_KeyDown;
this.KeyUp += MainWindow_KeyUP;
}
}
You have to call the second window from the first. This is something I did for a project where it popped up a new login panel window:
private void displayLoginPanel_Click(object sender, EventArgs e)
{
LoginPanel myLogin = new LoginPanel(this);
myLogin.Show();
this.Hide();
}
I used hide() instead of close() because you can see that I am sending a reference of the parent to the child LoginPanel in order to come back later. You can replace the Hide() with Close().
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();
I know you will be thinking "Not again this question", as I found like a hundred results when I searched for it. But when I put in the code as described on the pages here, it just minimizes to right above the start menu.
This is the code I use (I added a message box to see if the code gets triggered, but the message box never pops up):
private void Form1_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Because I don't know if it links to Form1 or Form, I have tried both, to no avail.
private void Form_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Now, when you double click on the Form, it puts this line in the Form1.Designer.cs:
this.Load += new System.EventHandler(this.Form1_Load);
Do I need a similar line to trigger the minimize event?
As you can see, I am completely lost :)
Oh, and it doesn't minimize to the taskbar, as I am using the following code to hide the form on run:
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
You need the event
private void Form1_Resize(object sender, EventArgs e)
{
}
Creating Event Handlers on the Windows Forms Designer
Add a NotifyIcon component to your Form. Make sure you set an icon via the properties pane otherwise it will be invisible.
Create an event handler for the form's Control.SizeChanged event. In that event handler place the following code:
sample code:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
ShowInTaskbar = false;
}
And then to make the form visible again the NotifyIcon.MouseDoubleClick event handler you can place the following code:
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
The basic thing you need to know is events. Events are triggered when certain things happen to your form (or any control). For example, when the form is resized, or loaded, or clicked, an event is raised. You can hook into this event to execute your own code when the event happens.
In your case you want to execute code to minimize the form, on the event that the form is resized. So you need to hook your method to the resize event. The name of your method is not relevant, so let's use a better name:
private void HideWhenMinimized(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
To hook your HideWhenMinimized method into the Resize event of the form, you have to do it like this:
this.Resize += new System.EventHandler(this.HideWhenMinimized);
If you add that line of code in the form's constructor or Load event, then your code gets called as soon as the form is resized.
How can I make the (default) Close button (at top right corner) in my application, to work it as Minimize.
Actually I want to minimize the application on clicking the cross-symbol, but exit the application, when use clicks on my menu option Exit.
I wrote this code for minimizing the form on clicking close button:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (minimize_on_close == "Yes")
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
and wrote this code for exiting the application, on clicking exit from menu options.
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Application.Exit();
}
But now, when I click on Exit menu option, then also the form is being minimized, and not exiting.
Can anyone please help?
Check to see whether FormClosingEventArgs.CloseReason is equal to CloseReason.UserClosing before deciding to minimize the window. Alternatively, compare for CloseReason.ApplicationExitCall.
From the documentation for CloseReason:
Members
...
UserClosing
The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
...
ApplicationExitCall
The Exit method of the Application class was invoked.
Try this
EDIT
May use Resize Event to do it,
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.Hide();
}
Then using the FormClosing Event to cancel Close and minimize the Form as below
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}