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().
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);
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");
}
Being a very first user in Windows Form Development I want to ask a simple question ...
I created a form(MainWindow.cs) within the solution which opens at the time of running that solution.
Latter I created a second form(SecondWindow.cs) and created a event so that it can be called from the first window by clicking a button.When the second window loded up the first window(MainWindow.cs) will be disabled.
Now I want to enable the MainWindow.cs when the second window is closed.
How to do That...
A simple solution I already have is to hide the MainWindow.cs and latter on closing the second window make a new object of first window and show it.But it is not a good way i think because there is already a object created by .net framework for first window automatically, so why we should create a new object of Mainwindow.cs .
Code Of First Window ( MainWindow.cs ) :
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow();
this.Enabled = false;
price.Show();
}
Code Of Second Window ( On closing SecondWindow.cs )
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
// what will goes here to make the MainWindow.cs to enable state
}
Use price.ShowDialog() to show second form as modal dialog. Main form will be disabled until you close second form:
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
using(SecondWindow price = new SecondWindow())
price.ShowDialog();
}
You can pass the main form as an owner to the second form
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow() { Owner = this };
this.Enabled = false;
price.Show();
}
Then you can reference it from the second form.
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
Owner.Enabled = true;
}
I have a small problem, when I open a new window in WPF like so:
private void button2_Click(object sender, RoutedEventArgs e)
{
var newWindow = new Main();
newWindow.Show();
}
If I try to use Application.Current.Shutdown(); at the end of it my entire application shuts down rather than just my first initial window. So my question would be is there a way to open a new window while closing the previous window safely?
Thanks :)
I'd do something like this:
//Somewhere in your class
YourOtherForm otherForm = null;
//then, on the event handler
private void button2_Click(object sender, RoutedEventArgs e) {
if((otherForm.IsDisposed) || (null == otherForm)) {
otherForm = new YourOtherForm();
// or, this is an MDI or something
// otherForm = new YourOtherForm(this);
// assuming you have an extra constructor to pass the parent
}
otherForm.Show();
this.Close(); // or this.Hide(); if it's the main form
}
Edit: I haven't tested this code tho..
The only way to do this is to run the program externally (I will find the code to do this shortly). Otherwise, anything that is created from within the main application will be destroyed when the parent shuts down.
The code to spin up a new program:
System.Diagnostics.Process.Start("program.exe");
You will need to change the ShutdownMode to OnLastWindowClose in your App.xaml.
if you run this snippet of code(put it in form1) in a fresh new winform app with 2 forms
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
this.Show();
newForm.ShowDialog();
}
private void click(object sender, EventArgs e)
{
((Form)((Control)sender).Parent).ShowInTaskbar = false;
}
and you click on the button on the new form(should be form2), form2 will close.
How to keep it open?
It is not possible. I actually filed a bug report about it at Microsoft's feedback site but they flipped me the bird on it.
Admittedly, it is a tricky problem to solve, changing the property requires Windows Forms to recreate the window from scratch because it is controlled by a style flag. The kind you can only specify in a CreateWindowEx() call with the dwExStyle argument. Recreating a window makes it difficult to keep it modal, as required by the ShowDialog() method call.
Windows Forms works around a lot of User32 limitations. But not that one.
You keep it open by setting ShowInTaskbar to false before you ShowDialog();
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
this.Show();
// add this line of code...
newForm.ShowInTaskbar = false;
newForm.ShowDialog();
}
private void click(object sender, EventArgs e)
{
((Form)((Control)sender).Parent).ShowInTaskbar = false;
}
Or just don't make the second form modal. This works also.
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
this.Show();
newForm.Show();
}
I don't know the specific mechanism here, but clearly what is happening is that changing the flag (which actually changes one or more WS_EX_xxx window styles) is causing the modal pump of ShowDialog() to exit. This, in turn is causing you to (finally!) exit Form1_Load and then your newForm goes out of scope and is destroyed.
So your problem is a compbination of ShowDialog() and the fact that you aren't prepared for ShowDialog() to ever exit.
Now a modal for shouldn't show up with a taskbar icon in the first place, there really should be only 1 taskbar icon for an application and all of it's modal forms, since when a modal form is running, the main form is disabled anyway. When the main form is minimized, all of the modal forms that it owns will be hidden, etc.
So if you really want this second form to be modal, you shouldn't give the user the ability to give it a taskbar icon. If using ShowDialog() was just test code, then don't worry about it. The problem will go away when the form runs on the main application pump.
Your question isn't very clear to me. Anyway, the newForm form is displayed as a modal dialog, which means that it blocks the user from working with the parent form until it is closed. Modal dialogs usually have some buttons that automatically close them returning either OK or Cancel to the calling form (as a return value of ShowDialog). This is done using the DialogResult property, so if this property is set for your button, this may be a reason why the modal form closes when you click on it.
If you want to show more forms in a way that allows the user to work with both of them, you'll need to use modeless dialog. Here is a good overview article on MSDN.
how... my... this is an ugly hack
this work
private void Form1_Load(object sender, EventArgs e)
{
Form2 newForm = new Form2();
Button b = new Button();
newForm.Controls.Add(b);
b.Click += new EventHandler(click);
newForm.FormClosed += new FormClosedEventHandler(form2_closed);
newForm.FormClosing += new FormClosingEventHandler(form2_closing);
this.Show();
do
{
newForm.ShowDialog();
} while (newForm.IsDisposed == false );
}
private void click(object sender, EventArgs e)
{
((Form)((Control)sender).Parent).ShowInTaskbar = !((Form)((Control)sender).Parent).ShowInTaskbar;
}
private void form2_closed(object sender, FormClosedEventArgs e)
{
((Form)sender).Dispose();
}
private void form2_closing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.None)
e.Cancel = true;
}