如何获取当前活动模态表单的引用?

时间:2020-12-20 15:49:16

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?

我正在编写一个小类来驱动win表单应用程序的集成测试。测试驱动程序类可以访问主Form,并查找需要按名称使用的控件,并使用它来驱动测试。要查找控件,我将遍历Control.Controls树。但是,当我想在对话框窗口(显示为对话框的自定义窗体)中获得控件时,我会陷入困境。我该如何掌握它?

3 个解决方案

#1


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

您可以使用静态Form.ActiveForm属性获取对当前活动表单的引用。

Edit: 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:

编辑:如果没有Form具有焦点,Form.ActiveForm将返回null。解决这个问题的一种方法是使用Application.OpenForms集合并检索最后一项,当使用ShowDialog显示时,将是活动表单:

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

#2


I'm not sure if you can access controls on a pre-built dialog box; they seem all packaged together. You may have more luck building a dialog box of your own that does what you want it to do. Then you can access the .Controls inside of it.

我不确定你是否可以访问预建对话框上的控件;他们似乎都打包在一起。您可以更幸运地构建一个自己的对话框来完成您希望它做的事情。然后你可以访问它内部的.Controls。

#3


Correct me if i'm wrong, though, it sounds as if you are possibly attempting to access the controls on the dialog form when it's not quite possible to.

如果我错了,请纠正我,但听起来好像你可能试图访问对话框表单上的控件时,它是不可能的。

What I mean is, ShowDialog will "hold up" the thread that the form was created on and will not return control to the application (or, your test class) until ShowDialog has finished processing, in which case your user code would continue on its path.

我的意思是,ShowDialog将“保持”创建表单的线程,并且在ShowDialog完成处理之前不会将控制权返回给应用程序(或您的测试类),在这种情况下,您的用户代码将继续路径。

Try accessing or manipulating the controls from a separate thread (in this case, refactor the test driver class to spawn a separate thread for each new form that must be displayed and tested).

尝试从单独的线程访问或操作控件(在这种情况下,重构测试驱动程序类,为每个必须显示和测试的新表单生成一个单独的线程)。

#1


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

您可以使用静态Form.ActiveForm属性获取对当前活动表单的引用。

Edit: 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:

编辑:如果没有Form具有焦点,Form.ActiveForm将返回null。解决这个问题的一种方法是使用Application.OpenForms集合并检索最后一项,当使用ShowDialog显示时,将是活动表单:

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

#2


I'm not sure if you can access controls on a pre-built dialog box; they seem all packaged together. You may have more luck building a dialog box of your own that does what you want it to do. Then you can access the .Controls inside of it.

我不确定你是否可以访问预建对话框上的控件;他们似乎都打包在一起。您可以更幸运地构建一个自己的对话框来完成您希望它做的事情。然后你可以访问它内部的.Controls。

#3


Correct me if i'm wrong, though, it sounds as if you are possibly attempting to access the controls on the dialog form when it's not quite possible to.

如果我错了,请纠正我,但听起来好像你可能试图访问对话框表单上的控件时,它是不可能的。

What I mean is, ShowDialog will "hold up" the thread that the form was created on and will not return control to the application (or, your test class) until ShowDialog has finished processing, in which case your user code would continue on its path.

我的意思是,ShowDialog将“保持”创建表单的线程,并且在ShowDialog完成处理之前不会将控制权返回给应用程序(或您的测试类),在这种情况下,您的用户代码将继续路径。

Try accessing or manipulating the controls from a separate thread (in this case, refactor the test driver class to spawn a separate thread for each new form that must be displayed and tested).

尝试从单独的线程访问或操作控件(在这种情况下,重构测试驱动程序类,为每个必须显示和测试的新表单生成一个单独的线程)。