Get a list of instances of open windows that are subtracted from different assemblies

I have a downloader application that loads a menu, and when the user clicks the menu image button, a list based on the text opens

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

if he clicks on the same button again and opens again, I would like to prevent this. but this is for a WPF application

+4
source share
5 answers

If you need a list of open forms, then Application.OpenForms . You can iterate over this using GetType () and check .Assembly to find those from another assembly. Other than that, I don't quite understand 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 keep track of the forms that you opened yourself, then cache this instance. Or, if you use "owned forms", you can simply 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); } 
+11
source
  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; } 
+2
source

Another simple example

 private Boolean FindForm(String formName) { foreach (Form f in Application.OpenForms) { if (f.Name.Equals(formName)) { f.Location = new Point(POINT.X, POINT.Y + 22); return true; } } return false; } 
+2
source

You can use the command template. The loader block will look for commands in loaded assemblies. For each command, the loader creates a menu item (or something else that you want), and the click event fires a specific command.

The team must know whether to create a new form or use existing ones.

0
source

Mark Garwell's answer helped me figure out what I had to do, but I needed to configure WPF.

(In my case, I wanted to close any windows that do not belong to 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(); } } 
0
source

All Articles