Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,
i develop a simple application and in my application user can reach to the form from more than one place , and that makes some times one form is opened more than one time
So i want to Make each Form Is opened or Showed only one instance from it created
can any one help me in doing that ..?
thanks for advanced
Posted

There is a number of methods you can search on Web under "single-instance" or "single application instance" with WPF. I'm using a pretty cunning and compact method using .NET remoting. I'm not sure I can explain it without publishing yet another article, but I'll try.

Please see my past solutions here:
Open File in already Open Program[^],
Open File in already Open Program[^].

—SA
 
Share this answer
 
It seems you should implement a mechanism to distribute your form to all the classes how should be able to show it. If your distribute the reference pointer, you will always get the same instance of the form.

So say you have a Form. You can create a instance of that in your main class. Then you pass it down to your sub-classes either in the constructor for simple apps, or as a property (FYI: you actually will then just copy the reference pointer to that form). You then use Show and Hide. It is now important that you override the closing of the form by handling the Closing event and setting the Cancel property of the CancelEventArgs to true, and calling Hide instead.
 
Share this answer
 
You can use the singleton design pattern[^] for that. That pattern ensures that there's only one instance of a class.

example code for a WPF Window:
C#
public sealed partial class SingleWindow : Window {
    private static SingleWindow instance;
    private bool isMainExit=false;
    private SingleWindow() {
        InitializeComponent();
        App.Current.MainWindow.Closed+=new EventHandler(MainWindow_Closed);
    }
    public static SingleWindow Instance {
        get {
            if (instance == null) {
                instance = new SingleWindow();
            }
            return instance;
        }
    }
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
        if (!isMainExit) {
            this.Hide();
            e.Cancel = true;
        }
        base.OnClosing(e);
    }
    private void MainWindow_Closed(object sender, EventArgs e) {
        this.isMainExit=true;
        this.Close();
    }
}


After you close a form/window that form/window enters a state where you can't use it anymore (until the garbage collecter removes it entirely, I suppose). Therefore you need to check wether the form/window has been closed. Forms get disposed so you check for IsDisposed. I couldn't immediately find a similar mechanism for a WPF Window so I implemented a workaround. If anybody knows a more elegant solution, please correct me. :-)

edit: replaced WinForms code by WPF code
 
Share this answer
 
v3
Comments
Yasser El Shazly 28-Jul-11 1:53am    
there is no Property called ISDisposed
Joris Janssens 28-Jul-11 5:01am    
It was code for WinForms not WPF. I edited my solution with an example for WPF.
Amund Gjersøe 28-Jul-11 6:48am    
I would say singleton should only be used when nothing else is possible. Singleton-classes introduce ambiguities when reading the code, since they look just the same as regular classes. Maintenance on code with lurking singletons is something near a nightmare.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900