I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.
Related
My program has a MainWindow and a SecondWindow, which is called by the first one like this:
SecondWindow config = new SecondWindow();
config.Owner = this;
config.Show();
Those lines are contained on a Button.Click method. And I want to check if it is already open, close it or do not open it.
Thanks!
Do not create a new instance. Just add it to top of your MainWindow class, and when you click the button, use secondWindow.Hide();. You must hide, because if you close it, you can't show it again. If you want to do not open it, activate the window and take it to top of desktop with secondWindow.Activate();.
Try this:
if(Application.Current.Windows.OfType<SecondWindow>().FirstOrDefault() == null)
{
//second window not exist
}
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 a Xamarin/MonoMac project with 2 windows. The main window opens, and functions like it should. However, I have a button that needs to open a second window (Lets call the first window "Main", and the second window "Second"). Both have their respective XIB files, and controller classes.
The button is set up in MainWindowController, and functions normally such that:
secondButton.Activated += (o, e) => {
//Do stuff
}
Methods such as Console.WriteLine(); function as expected when the button is clicked.
The second window controller class is set up such that:
public partial class SecondWindowController : MonoMac.AppKit.NSWindowController {
...
}
What I need to do is cause secondButton to open an instance of SecondWindow upon being clicked, while passing the active instance of MainWindow to the SecondWindow constructor.
In C#.NET I would just use
SecondWindow sc = new SecondWindow(this);
sc.ShowDialog();
I can get SecondWindow to open, using this
NSWindowController nsc = new NSWindowController("SecondWindow", this);
nsc.ShowWindow(this);
But that causes an error stating
"Unknown window Class SecondWindow in Interface Builder file, creating a generic window instead."
The window shows as it should display, but no functions in SecondWindowController function, even if I run
nsc.AwakeFromNib();
directly after opening the window. No console output, and none of the buttons work.
How might I get the window to open, and function properly? If more information is needed, please let me know what I need to add, so I can get that to you.
Thanks in advanced!
To get the window from the Storyboard you would need to use NSStoryboard.InstantiateControllerWithIdentifier(string id) as NSWindowController
Also you must set id as identifier for the window in the storyboard.
In the program I am trying to build, I have a menu button that opens a second window. The user puts information into the second window, presses the "Done" button, and the information is transfered into the main window. The problem I am having is opening the second window. I have both windows build in xaml files in Visual Studio but I can't find a way to show the second window. Using "Window window = new Window" does not fit my needs because 1) I already have the second window built and 2) I have tried this and I cannot figure out how to add children to the window; there is no window.children nor any grid to put the children into. Thank you in advance!
Moments after I pressed post, I thought of something I hadnt tried:
"WindowAdd add = new WindowAdd; //WindowAdd being the second window
add.Show();"
This does exactly what I want it to do. The next problem I have is sending the information the TextBoxes into the MainWindow. I am thinking cookies might work but am unsure. Anyone have any thoughts? Thanks in advance!
You need to create the Window in code, but instead of doing:
Window window = new Window();
You should use:
Window2 window = new Window2(); // Assuming the window's class name is Window2
This will construct and initialize an instance of your new window class, defined in XAML. Once you've done this, you can open the window and you'll see all of your controls.