views:

12505

answers:

5

I have a 'loader app' that loads a menu and when user clicks the menu image button a list view opens based on the text

(if text = employee)
(Go to class A) (Go to class B) ... ... (Show List View Window)

if he clicks again on the same button it opens again, I would like to prevent this.Any ideas?

i.e but this for a WPF application

+4  A: 

If you want a list of the open forms, that is Application.OpenForms. You could iterate over this, using GetType() and checking the .Assembly to find those from a different assembly. Beyond that, I'm not entire clear on the question...

        Assembly currentAssembly = Assembly.GetExecutingAssembly();
        List<Form> formsFromOtherAssemblies = new List<Form>();
        foreach (Form form in Application.OpenForms) {
            if (form.GetType().Assembly != currentAssembly) {
                formsFromOtherAssemblies.Add(form);
            }
        }

If you just want to track forms you have opened yourself, then cache that instance. Or if you use "owned forms", you can just check by name:

    private void button1_Click(object sender, EventArgs e) {
        foreach (Form form in OwnedForms) {
            if (form.Name == "Whatever") {
                form.Activate();
                return;
            }
        }
        Form child = new Form();
        child.Name = "Whatever";
        child.Owner = this;
        child.Show(this);
    }
Marc Gravell
First,thank you for your reply.With reference to your first code snippet.The issue is Application.OpenForms (does not have the forms thatare displayed,it is just a loader) and well Assembly currentAssembly = Assembly.GetExecutingAssembly();looks promising... code should be in reverse..mmmm
abmv
Application.OpenForms will return 0 i.e since it is loader
abmv
I'm not sure why your main form just being a loader should affect the outcome here... if the runtime knows about the form, it should appear in Application.OpenForms - unless you mean it is running in a different *process* - is that it?
Marc Gravell
the loader is not a main form its a exe that displays a UI (menu) and has no idea what it is displaying. so I need to get the executing assembly but then what? There is some abstraction involved in the code...is it possible to find open forms any other way ?
abmv
A: 

You can use a Command pattern. The loader assembly will search for commands in loaded assemblies. For every command the loader create menu item ( or anything else, you want ), and click event will run the concrete command.

The command must know if should be created new form or used some already existing.

TcKs
that exactly is the question
abmv
+1  A: 
    NewProduct newproduct;
    private void button1_Click(object sender, EventArgs e)
    {

        if(!isOpened())
        {
            newproduct = new NewProduct();
            newproduct.Show();
        }

    }
    private bool isOpened()
    {
        foreach (Form f in Application.OpenForms)
        {
            if (f == newproduct)
            {
                return true;
            }
        }
        return false;
    }
joakim
Please create a WPF Application, and try this.I just tried it didn't work.
abmv
A: 

how to get last form object if two same forms are opened from main menu form. e.g first form = Company Form Second form = Company Form

i want second form's object,so that its FormClosing Event will be fire.

Shekhar
You should probably ask this as a new question, not post it here as an answer. More people would look at it that way. The "Ask Question" button is in the top right. Try to explain as clear as possible what you are trying to do and where your problems are.
sth
A: 

Mark Garvell's answer helped me to figure out what I should do, but it needed adjusting for WPF.

(In my case I wanted to close any windows not owned by the main one when it closes, but the principle is the same.)

private void EmployeeMenuItemClick(object sender, RoutedEventArgs e)
{
    bool found = false;
    foreach(Window w in Application.Current.Windows)
    {
        if(w.GetType() == typeof(EmployeeListViewWindow)
        {
            found = true;
            break;
        }
    }
    if(!found)
    {
        EmployeeListViewWindow ew = new EmployeeListViewWindow();
        ew.Show();
    }
}
Matt Ellen
could it be adjusted?
abmv
Could what be adjusted? The new/current window? Yes, just set its properties.
Matt Ellen