我怎样才能获得引用当前活动模式的形式? [英] How can I get the reference to currently active modal form?

查看:143
本文介绍了我怎样才能获得引用当前活动模式的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个小班用于驱动一个双赢的形式应用的集成测试。测试驱动程序类访问的主要形式,并查找需要使用的名字,并用它来驱动测试控制。为了找到我遍历Control.Controls树控制。不过,我碰到困难时,我想,以控制在一个对话框窗口(显示为一个对话框,自定义表单)。我怎样才能得到它持有?

I am writing a small class for driving integration testing of a win form application. The test driver class has access to the main Form and looks up the control that needs to be used by name, and uses it to drive the test. To find the control I am traversing the Control.Controls tree. However, I get stuck when I want to get to controls in a dialog window (a custom form shown as a dialog). How can I get hold of it?

推荐答案

您可以通过使用静态<一有了一个指向当前活动窗体href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activeform.aspx"><$c$c>Form.ActiveForm属性。

You can get a reference to the currently active form by using the static Form.ActiveForm property.

编辑:如果没有表格的焦点, Form.ActiveFor​​m 返回
要解决这个问题的方法之一是使用 Application.OpenForms 收集和检索的最后一个项,女巫将成为活动表格当使用的ShowDialog 显示它:

If no Form has the focus, Form.ActiveForm will return null.
One way to get around this is to use the Application.OpenForms collection and retrieve the last item, witch will be the active Form when it is displayed using ShowDialog:

// using Linq:
lastOpenedForm = Application.OpenForms.Cast<Form>().Last()
// or (without Linq):
lastOpenedForm = Application.OpenForms[Application.OpenForms.Count - 1]

这篇关于我怎样才能获得引用当前活动模式的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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