Reputation: 3041
In a WPF window, how do I know if it is opened?
My goal to open only 1 instance of the window at the time.
So, my pseudo code in the parent window is:
if (this.m_myWindow != null)
{
if (this.m_myWindow.ISOPENED) return;
}
this.m_myWindow = new MyWindow();
this.m_myWindow.Show();
EDIT:
I found a solution that solves my initial problem. window.ShowDialog();
It blocks the user from opening any other window, just like a modal popup. Using this command, it is not necessary to check if the window is already open.
Upvotes: 47
Views: 71928
Reputation: 1
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static WindowNew? windowNew;
public MainWindow()
{
InitializeComponent();
}
private void BtnOpenWindow_Click(object sender, RoutedEventArgs e)
{
//if the window is null, create
if (windowNew == null)
{
windowNew = new WindowNew
{
Owner = this
};
windowNew.Show();
}
else
{
//else if not null, but cant be activated
if (!windowNew.Activate())
{
//failed, set to null
windowNew = null;
//call rekursiv this method again
BtnOpenWindow_Click(sender, e);
}
}
}
}
Upvotes: -1
Reputation: 141
This works if you want to Check if the Second Form is already Open and avoidt opening it again on buttong Click.
int formcheck = 0;
private void button_click()
{
Form2Name myForm2 = new Form2Name();
if(formcheck == 0)
{
myForm2.Show(); //Open Form2 only if its not active and formcheck == 0
// Do Somethin
formcheck = 1; //Set it to 1 indicating that Form2 have been opened
{
{
Upvotes: 0
Reputation: 1
public bool IsWindowOpen<T>(string name = "") where T : Window
{
return Application.Current.Windows.OfType<T>().Any(w => w.GetType().Name.Equals(name));
}
Upvotes: -1
Reputation: 9713
Here is another way to achieve this using LINQ.
using System.Linq;
...
public static bool IsOpen(this Window window)
{
return Application.Current.Windows.Cast<Window>().Any(x => x == window);
}
Usage:
bool isOpen = myWindow.IsOpen();
Upvotes: 10
Reputation: 1
void pencereac<T> (int Ops) where T : Window , new()
{
if (!Application.Current.Windows.OfType<T>().Any()) // Check is Not Open, Open it.
{
var wind = new T();
switch (Ops)
{
case 1:
wind.ShowDialog();
break;
case 0:
wind.Show();
break;
}
}
else Application.Current.Windows.OfType<T>().First().Activate(); // Is Open Activate it.
}
Kullanımı:
void Button_1_Click(object sender, RoutedEventArgs e)
{
pencereac<YourWindow>(1);
}
Upvotes: 0
Reputation: 191
If you need to activate the window if it is found, you can use the code below:
//activate a window found
//T = Window
Window wnd = Application.Current.Windows.OfType<T>().Where(w => w.Name.Equals(nome)).FirstOrDefault();
wnd.Activate();
Upvotes: 2
Reputation: 698
Is that you search ?
if (this.m_myWindow != null)
{
if (this.m_myWindow.IsActive) return;
}
this.m_myWindow = new MyWindow();
this.m_myWindow.Show();
If you want a singleton, you should read this : How can we create a Singleton Instance for a Window?
Upvotes: 1
Reputation: 43596
In WPF
there is a collection of the open Windows
in the Application
class, you could make a helper method to check if the window is open.
Here is an example that will check if any Window
of a certain Type
or if a Window
with a certain name is open, or both.
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
Usage:
if (Helpers.IsWindowOpen<Window>("MyWindowName"))
{
// MyWindowName is open
}
if (Helpers.IsWindowOpen<MyCustomWindowType>())
{
// There is a MyCustomWindowType window open
}
if (Helpers.IsWindowOpen<MyCustomWindowType>("CustomWindowName"))
{
// There is a MyCustomWindowType window named CustomWindowName open
}
Upvotes: 97
Reputation: 22794
Put a static bool in your class, named _open
or something like that.
In the constructor then do this:
if (_open)
{
throw new InvalidOperationException("Window already open");
}
_open = true;
and in the Closed event:
_open = false;
Upvotes: 2
Reputation: 6445
You can check if m_myWindow==null
and only then create and show window. When the window closes set the variable back to null.
if (this.m_myWindow == null)
{
this.m_myWindow = new MyWindow();
this.m_myWindow.Closed += (sender, args) => this.m_myWindow = null;
this.m_myWindow.Show();
}
Upvotes: 15