In pseudocode, this is what I am trying to do from a main window, with many non-modal sub-windows that can be opened and closed independently of the main window.
(Think "preferences" or "find")
On pressing "OPEN WINDOW"
STEP 1: If window does not exist, create it.
STEP 2: Window now exists, so bring it to the front & make it visible.
(Step 2 is NB in case OPEN WINDOW is pressed while window is already open - I don't want multiple instances of it, just bring it to the front.)
On pressing "CLOSE WINDOW"
STEP 3: Close the window
ALT STEP 3: Hide the window
This is the code I have tried. I got as far as being able to open the window, and bring it to the front if OPEN WINDOW is pressed again while the window is open. However, once I close the window, I CANNOT get it to open a second time. I get an error stating that Window.Show() cannot be used once the window is closed.
public static void OpenWindowOnce(Window windowToOpen)
{
foreach (Window n in Application.Current.Windows)
{
//Checks if the window is already open, and brings it to the front if it is
if (n.GetType() == windowToOpen.GetType())
{}
else
{ windowToOpen.Show(); }
}
windowToOpen.Activate();
}
Where am I going wrong in my code/logic? Thank you, I am pretty new to coding and have spent weeks trying to get this right.
You cannot use a Window that has been closed because its resources are disposed at that time. The solution is to simply create a new Window each time that you want to display it.
I can see that you are passing in a Window object and then trying to find the particular type of Window... in this case, you can use reflection to instantiate your Windows from their Type. In particular, please see the Activator.CreateInstance Method page on MSDN. You could use it something like this:
public static void OpenWindowOnce(Window windowToOpen)
{
foreach (Window n in Application.Current.Windows)
{
//Checks if the window is already open, and brings it to the front if it is
if (n.GetType() == windowToOpen.GetType()) { ... }
else
{
windowToOpen = Activator.CreateInstance(typeof(YourWindow)) as YourWindow;
windowToOpen.Show();
}
}
windowToOpen.Activate();
}
Related
I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.
I'm pretty new to WPF and I'm trying to make a database system. What I currently have is a Login Window. When you enter the user and password you are supposed to go to another window StudentInfoSystem . The code I used is pretty basic and common.
var info = new StudentInfoSystem.MainWindow();
info.Show();
this.Close();
So, what this would do, is after you press the login button, you get transferred to StudentInfoSystem and the login window closes. The problem I have with Show() is that it opens a window and immediately returns, right? It doesn't wait for the new window to close. So, my question is how can I open a new window and WORK with it? When I say work, I meant to show out information in my textboxes (In the NEWLY opened window) depending on the role the user has, etc...
I'm guessing the above code is in the button click handler for the Login Window, which would make the Login Window the parent of the StudentInfoSystem window.
Since WPF will close the parent and any child(ren) window(s) when closing the parent window, your StudentInfo window will also close when calling
this.Close();
One option might be to instead call
this.Hide();
but without seeing how the rest of your app is set up, not 100% sure this is the best approach.
Perhaps see these SO questions:
wpf-create-sibling-window-and-close-current-one
how-to-close-current-window-in-code-when-launching-new-window
Try window.Activate() to focus the new window and/or [any element].Focus() to focus any element within window.
As I understand, this should do what you want:
info.ShowDialog();
You could also check ShutdownMode property. I would rather say, login window is sth you want to close after login, but do what you want :). Usage of ShutdownMode property:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
}
}
I have this click event in my main window to open a new window
private void Button_Click(object sender, RoutedEventArgs e)
{
cm = new CanalesMain();
cm.Show();
cm.Canales.setValues();
}
My cm variable is defined as a member class in my main window because I need to load/refresh the setValues() method every 5 minutes (there's a TimeSpan and a EventHandler for that)
The thing is, in my "refresh data" method I have this if statement to ask if the cm variable is loaded and is not null (I mean, if the window was ever opened or if is opened, ask if isn't closed)
if (cm!=null && cm.IsLoaded)
{
cm.Canales.setValues();
}
Is this the correct or best way to ask if my window is open?
Strictly speaking no, it's not the right way. IsLoaded doesn't mean that Window is visible, just loaded (even if this may be equivalent in most of scenarios but it means this window has been created once, it has a handle, no mention to its visibility).
What you have to check is the Visibility property (it's what, finally, Show() will change), it'll be Visible if the Window is currently visible or Hidden if it has not been loaded (or it has been loaded and it still is but actually hidden).
To summarize:
if (cm != null && cm.Visibility == Visibility.Visible)
{
}
Please note that if Window is visible then it's implicit it has been loaded (it has a handle) but it's not true the vice-versa (a loaded window may not be visible and maybe it was even not in the past).
There is another way of checking which Windows are currently active:
foreach (Window window in Application.Current.Windows)
{
// Check for your Window here
}
If your Window is of a particular type, then you can do this instead:
foreach (Window window in Application.Current.Windows.OfType<YourWindow>())
{
// Do something with your Window here
}
Your Window will not appear here before it is displayed.
I think extension method will be very useful in that case, try this:
public static class WindowsExtensions
{
public static bool IsOpened(this Window window)
{
return Application.Current.Windows.Cast<Window>().Any(x => x.GetHashCode() == window.GetHashCode());
}
}
Which gives you ability to make call on every window like this:
var wind = new ChildWindow();
wind.ShowDialog();
var isOpened = wind.IsOpened();
Also you can check out this: How do I know if a WPF window is opened
More info about Application.Windows
If you call myWindow.Show() and myWindow.Close(), myWindow.IsLoaded should get you a value that you can use to indicate if the window is open or not.
Is there a way to open a new window instance within the primary application window and still wait for a user response before continuing with the main program? I just have a 3 option query with a picture to be offered to the user and a whole new window opening up seems like overkill, I would much prefer it opening in a frame within the current window.
Do something like this sans the separate window:
OptionWindow optionDialog = new OptionWindow();
optionDialog.Owner = this;
optionDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
optionDialog.ShowDialog();
if (optionDialog.DialogResult == true)
{
something;
{
Seems like you could just have your "OptionWindow" be a frame that is on the WPF form, and when you want to show it, just disable all other panels and show the frame with your dialog, and after the user completes the dialog, enable all other panels and hide your dialog frame.
After I posted my answer, I saw that Mark Hall made an edit to his comment with pretty much the same suggestion. Sorry Mark.
I guess you are looking for modal window which can be achieved using Window.Showdialog.
Opens a window and returns only when the newly opened window is
closed.
I'm creating a wpf application in c#, I know to close/open a window you have to use the .Close() and .Show() methods but for some reason the home screen, the first window that appears when I launch the application, won't close.
Home window1 = new Home();
window1.Close();
Name window2 = new Name();
window2.Show();
Window2 appears, but window1 won't close. What's the problem.
Where is your code for showing window1? If you show your home window somewhere else in your code, you need to use that reference in order to close it. Making a new Home object and calling its Close method will not close a window shown using another Home object.
Presumably because if you close the window you'll close the application.
If you just want to hide the main window use the window.Hide() method.
This from the help on Window.Close:
A Window can be closed using one of
several, well-known, system-provided
mechanisms located in its title bar,
including:
ALT+F4.
System menu | Close.
Close button.
A Window can also be closed using one
of several well-known mechanisms
within the client area that are
provided by developers, including:
File | Exit on a main window.
File | Close or a Close button on a
child window.
UPDATE
Tormod Fjeldskår has a good point in his answer. I assumed that the code was given as an example rather than being what was actually being used.
This is a bug in WPF. Window.Close will fail silently if the SourceInitialized event has not yet occurred. Subsequent calls to Window.Close will also fail.
https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=299100
For a workaround, add this to your Window:
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// check if we've already been closed
if (m_bClosed)
{
// close the window now
Close();
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
// make sure close wasn't cancelled
if (!e.Cancel)
{
// mark window as closed
m_bClosed = true;
// if our source isn't initialized yet, Close won't actually work,
// so we cancel this close and rely on SourceInitialized to close
// the window
if (new WindowInteropHelper(this).Handle == IntPtr.Zero)
e.Cancel = true;
}
}
bool m_bClosed;
Or you could have Window2 be the main window (you can change this in app.xaml in the StartUpUri property) and either have Window2 show and close Window1 or not show Window1 at all.
<Application x:Class="Invitrogen.TheGadget.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window2.xaml">
</Application>