获取从不同程序集中执行的打开的Windows窗体实例的列表 [英] Get list of open windows form instance that are excuted from different assembly
问题描述
我有一个用于加载菜单的加载器应用,当用户单击菜单图像按钮时,将基于文本打开列表视图
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)
如果他再次单击再次打开的相同按钮,我想防止这个。
,但对于WPF应用程序来说是这样的
if he clicks again on the same button it opens again, I would like to prevent this. i.e but this for a WPF application
推荐答案
如果您想要打开表单的列表,则为 Application.OpenForms
。您可以使用GetType()并检查 .Assembly
来进行遍历,以从其他程序集中找到它们。除此之外,我还不清楚这个问题...
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);
}
这篇关于获取从不同程序集中执行的打开的Windows窗体实例的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!