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
}
Related
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.
I want to open a POP up on a context menu button click in C#.Please help.
And thanks in advance
You need to create a new Window class. You can design that then any way you want. You can create and show a window modally like this:
MyWindow popup = new MyWindow();
popup.ShowDialog();
You can add a custom property for your result value, or if you only have two possible results ( + possibly undeterminate, which would be null), you can set the window's DialogResult property before closing it and then check for it (it is the value returned by ShowDialog()).
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.
I am using MDIParent window form which contains menus, when I click on same menu again it open a new window. so how to stop this from reopening the window if it is already open? It should not display window form every time on click.
Use Application.OpenForms property.
Boolean found =
Application.OpenForms.Cast<Form>().Any(form => form.ID == "TargetFormID"
if (!found)
{
// Open a new instance of the form //
}
2 ways:
Way 1, flags:
Keep a flag (or list of flags) for the open forms.
Each time you open the form (create a new() one) set the flag to "true".
When the form closes, set the flag to false.
In the button's click event, check the flag to see if the form is open before creating a new one.
Way 2, keep a reference:
Keep a reference in the main form to all the forms you're using.
Initialize them as null when the forms aren't open.
When you open a new form set the reference to it.
On the button's click event check if the form's reference is null before you create a new one.
I prefer the second way. It's easier to control your resources when you have references to all your sub-forms.
You could maintain a list of open forms (and check the list in the onClick event), or disable/enable the menu item when the form opened ot closed.
Another why would be to create a Property in the Form which keeps the default instance you use.
private static Form _defaultInstance;
public static Form DefaultInstance()
{
get {
if(_defaultInstance == null || _defaultInstance.IsDisposed)
{
_defaultInstance = new yourTypeHere();
}
return _defaultInstance;
}
}
And now you always access your window through this property:
yourTypeHere.DefaultInstance.Show();
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.