Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent multiple instances of windows

Tags:

wpf

How could I prevent opening multiple windows from a wpf application?

I need to open a window from the menu and if I click to open it again, I want the already opened window to become active.

Any suggestions?

like image 221
profanis Avatar asked Sep 19 '25 19:09

profanis


1 Answers

You could use the Application.Current.Windows collection for that. Just check whether this collection contains the window you are about to open and if it does, activate it, otherwise create new window and show it:

var existingWindow = Application.Current.Windows.Cast<Window>().SingleOrDefault(w => /* return "true" if 'w' is the window your are about to open */);

if (existingWindow != null) {
    existingWindow.Activate();
}
else {
    // Create and show new window
}
like image 165
Pavlo Glazkov Avatar answered Sep 23 '25 12:09

Pavlo Glazkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!