如何从所有打开的表单中调用方法 [英] How to call method from all opened forms

查看:81
本文介绍了如何从所有打开的表单中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以所有打开的表单调用方法

I want call a method in the all opened forms

此代码在最后一个csutomer打开的表单上调用MyUpdateFnc():

this code call MyUpdateFnc() on the last csutomer Opened Form:

if (Application.OpenForms["frmCustomer"] != null)
{
     (Application.OpenForms["frmCustomer"] as frmCustomer).MyUpdateFnc();
}

可能有几种形式。

推荐答案

您可以使用 Application.OpenForms.OfType< T> 方法获取所需类型的所有形式。接下来,您需要遍历表单集合。例如,使用 foreach 循环,如下所示:

You could get all forms of desired type using the Application.OpenForms.OfType<T> method. Next you need to iterate through the forms collection. For instance, using the foreach loop as follows:

foreach (frmCustomer frm in Application.OpenForms.OfType<frmCustomer>())
    frm.MyUpdateFnc();

这篇关于如何从所有打开的表单中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆