Babak.Abad
Babak.Abad

Reputation: 2956

Avoid opening same window twice WPF

I'm writing a program using WPF(C#). I use method like this to open and close windows:

public static void openCloseWindow(Window toBeOpen, Window toBeClose)
{
    toBeOpen.Show();

    makeWindowCenter(toBeOpen);

    toBeClose.Close();
}

In part of program I use this method like this:

openCloseWindow(new BestCustomerWindow,this);

so it is possible to end-user click several times on a button and many windows can open.

is there any way to avoid opening a windows while it is running?

for more information:

lets I click on a button which is open window1. I want to:

Upvotes: 4

Views: 11292

Answers (6)

riftiaudra
riftiaudra

Reputation: 1

10y late, but hope useful for someone :

            var winname = "MYWINDOW";

            //Validate
            bool iswinopen = false;

            foreach (Window w in Application.Current.Windows)
            {
                if (w.Name != winname) continue;

                iswinopen = true;
                w.Activate();

                break;
            }

            if (iswinopen) return;

            //Set
            var window = new Window() { Name = winname };

            window.ShowInTaskbar = true;
            window.Owner = Application.Current.MainWindow;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.WindowState = WindowState.Normal;
            window.WindowStyle = WindowStyle.SingleBorderWindow;

            window.Content = new MyView() { DataContext = this }; //im use usercontrol as view with viewmodel here
            window.Show();

Upvotes: 0

HelloWindowsPhone
HelloWindowsPhone

Reputation: 700

I wrote a quick function base on @rhys-towey answer

    void OpenNewOrRestoreWindow<T>() where T : Window, new()
    {
        bool isWindowOpen = false;

        foreach (Window w in Application.Current.Windows)
        {
            if (w is T)
            {
                isWindowOpen = true;
                w.Activate();
            }
        }

        if (!isWindowOpen)
        {
            T newwindow = new T();
            newwindow.Show();
        }
    }

Usage OpenNewOrRestoreWindow<SomeWindow>();

Upvotes: 1

Srikanth Puli
Srikanth Puli

Reputation: 1

just use

tobeopen.ShowDialog();    

example my window class name is "Window1" use "Window1.ShowDialog();"

if the window is already opened it wont open and it makes alert sound.

Upvotes: -2

Rhys Towey
Rhys Towey

Reputation: 2385

Replace WINDOWNAME with the name of the desired Window:

bool isWindowOpen = false;

foreach (Window w in Application.Current.Windows)
{
    if (w is WINDOWNAME)
    {
        isWindowOpen = true;
        w.Activate();
    }
}

if (!isWindowOpen)
{
    WINDOWNAME newwindow = new WINDOWNAME();
    newwindow.Show();
}

Upvotes: 12

Ofir
Ofir

Reputation: 5319

This code will do exactly what you are asking for:

Just store the dialog object and check whether it's already been created in showWindow.

Used the windows Closed event to clear the reference to the dialog object.

AddItemView dialog;

private void showWindow(object obj)
{

    if ( dialog == null )
    {
       dialog = new AddItemView();
       dialog.Show();
       dialog.Owner = this;
       dialog.Closed += new EventHandler(AddItemView_Closed);
    }
    else
       dialog.Activate();
}

void AddItemView_Closed(object sender, EventArgs e)
    {

        dialog = null;
    }

Please notice that this question already asked here

Upvotes: 1

Mark Yu
Mark Yu

Reputation: 11

Abad,

I think you can use Mutex, please refer below code(in App.xaml.cs file):

public partial class App : Application
{
    [DllImport("user32", CharSet = CharSet.Unicode)]
    static extern IntPtr FindWindow(string cls, string win);
    [DllImport("user32")]
    static extern IntPtr SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32")]
    static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32")]
    static extern bool OpenIcon(IntPtr hWnd);

    protected override void OnStartup(StartupEventArgs e)
    {
        bool isNew;
        var mutex = new Mutex(true, "My Singleton Instance", out isNew);
        if (!isNew)
        {
            ActivateOtherWindow();
            Shutdown();
        }
    }
    private static void ActivateOtherWindow()
    {
        var other = FindWindow(null, "MainWindow");
        if (other != IntPtr.Zero)
        {
            SetForegroundWindow(other);
            if (IsIconic(other))
                OpenIcon(other);
        }
    }
}

Upvotes: 1

Related Questions