Close another window in WPF - c#

I'm building a multi-window app.
The problem I've got, I can't close "Window1" or "Window2" from button in "MainWindow".
All windows are in the same app.
Is there a way to kill another window?

Sure there is. If you have a reference to that Window, just call the Close() method.
Window window1 = new Window1();
window1.Show();
...
window1.Close();

Related

Close Window after open another window?

I'm work on WPF project, let say I have two windows window1,window2, window1 call window2: in window1 code behind :
Window2 _window2 = new Window2();
_window2.ShowDialog();
What I want to know how I close first window (window1) after the secound window (window2) is opened ?
If "Window1" is not your main window you can just type this
Window2 _window2 = new Window2();
_window2.Show();
this.Close();
If your form is the main form than refer
(Windows Forms: Change application mainwindow at runtime)
Before you can close the main window (if it is), then you need to tell the app first which is the main window. Depending upon your shut down mode, if you close the main window, you shut down the app. You can set this using Application.Current.MainWindow property. See Here.
If you wish to close the first window after opening the second, you will need to hook up an event, or better still use a decoupled messaging system to inform the first window the second has opened.
Form1:
Window2 _window2 = new Window2();
this.Hide(); //Hides Form 1
_window2.ShowDialog();
Also see:
Close a Window from Another In Wpf
if you wanna have window1 closed.

How do I open my closed form through the notifyicon in the taskbar?

Currently I am developing a windows form application in c# that has several forms.
I am running a background form that operates the notifyicon property that allows the icon to appear in the taskbar.
When I launch the program, it will launch a loginForm, after which logging in it will go into a mainForm. After closing the mainForm, the application does not close yet, which in this case works like Windows Live Messenger.
How do I make my program in a way that after I the mainForm, through double clicking it will bring the form back up? (Like how MSN works.)
Or is it a better solution for me to close the whole application when I press the X button in the title bar. Which brings up another problem for me as I cant seem to exit the application when I close other forms other than the main form.
Probably you have NotifyIcon on your main form. Subscribe on the DoubleClick event of this control and change state of your main form in the handler:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
Just set the Visible property of the form to true/false. Or you could call Show()/Hide().

Wpf Main Window Get Minimized

How can i keep my main window maximized in wpf app.
when i open second window and do something in that and close it, my main window get minimized.
please help me.
in fact i have three window:
A main window that application start with that.
A second window that shows a list of entities. with edit, delete and add button.
The third window that is for editing the selected entity.
when i close the third and second window, my main window get minimized.
i use the below code for opening second and third windows:
SecondWindow win = new SecondWindow();
win.Owner = this;
win.ShowDialog();
thanx.
I think Your problem is: something about how you show Main-dialog not other Forms!. Can you provide how Show Main-dialog?
also you can try use WindowState="Maximized" on your Main-dialog.

C# WPF child window (about window)

I have an application on C#/WPF that I`m developing ... I have a btn named About which should open a new window that contains details about the application or whatever I will put in it.
When I click the btn, a new window (about) opens, when I click again, while the new window (about) is opened another one is opened, how can I prevent this from happening, I also want the application to be disabled when the About window is opened and to be enabled when the About window is closed, Like most of the applications when about is clicked.
You should use the ShowDialog Method: http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Code Sample:
// Instantiate window
AboutWindow aboutWindow = new AboutWindow();
// Show window modally
// NOTE: Returns only when window is closed
Nullable<bool> dialogResult = aboutWindow.ShowDialog();
Just use ShowDialog() method instead of Show()
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();

csharp winform modal window, able to click on main window

language c#, winform
how do i create a modal window or such a thing that when it still showing
i would still be able to click or interact with the main window..
thanks.
put some code please or links..
Make the dialog non-modal (use Show instead of ShowDialog), and make it top-most (TopMost = true)
Just use the overload of Form.Show() that takes a form as a parameter, like this:
Form f = new Form();
f.Show(this);
This will keep the form always on top of the form that calls it, but still let you click and access the calling form.
Some confusion here I think;
Modal is when the window blocks the underlying window, and must be closed to enable the underlying window to regain control. Form.ShowDialog(owner) is used to accomplish this.
Non-Modal is a window that is opened "in parallell" to the underlying window. Both windows can be accessed and respond to mouse and key events. Form.Show(owner) to accomplish this.
Modality by definition means that you are not able to click anywhere else. You can create another form and show it with Show() method.
Show() Method allows you to click anywhere while ShowDialog() won't

Categories