关闭dll中的所有表单。
问题描述:
亲爱的,
在我的申请中,我打电话给表格A,形成一个电话表格B(dll中的A和B)。当应用程序引发错误时,我想关闭在引用dll中调用的所有表单。怎么能?
在应用程序中,.NET有方法End或Application.Exit(),但我不知道在dll中,如何关闭dll中的所有引用但不关闭我的应用程序。请帮我。
谢谢大家。
Dear all,
In my application, I call form A, form A call form B (A and B in the dll). I want to close all form called in reference dll when application raise error. How can?
In Application, .NET have method End or Application.Exit(), but I don't know in dll, how to close all reference in dll but don't close my application. Please help me.
Thanks all.
答
没有直接的方法可以做到这一点。
但是,你可以添加你自己 - 这并不困难。
在DLL中创建一个静态列表(可能值得添加静态类DLLControl或类似的)Form对象。
每次其中一个表单获取Load事件,将其添加到静态列表,并向FormClosing事件添加处理程序。
在FormClosing处理程序中,检查表单是否在静态列表中,如果有的话将其删除。
现在,向DLL添加一个静态方法:
There is no direct way to do that.
However, you could add it yourself - it's not that difficult.
Create a static list within the DLL (it's probably worth adding a static class "DLLControl" or similar for this) of Form objects.
Each time one of the forms gets a Load event, add it to the static list, and add a handler to the FormClosing event.
In the FormClosing handler, check if the form is in the static list, and if so remove it.
Now, add a static method to the DLL:
public static CloseAllForms()
{
List<Form> openForms = staticListOfForms;
staticListOfForms = new List<Form>();
foreach (form f in openForms)
f.Close();
}