如何对BaseForm类中的特定表单执行操作? [英] How to perform action for specific forms in the class of the BaseForm?
问题描述
我想让我的程序知道打开了哪个表单,然后对该表单进行一些特定的操作.表单在带有概述的主窗口中打开,用户可以单击图像以打开新表单.该主窗口不是我要打开的表单的BaseForm.
要检查打开了什么形式,我尝试了以下代码:
I want to let my program know which form is opened and then do some specific action for that form. Forms are opened in a main-window with an overview and the user can click on an image to open a new form. This main-window is not my BaseForm for the forms I want to open.
To check what form is open, I tried the following code:
private void FormCheck()
{
foreach (Form form in Application.OpenForms)
{
if (form is thisOne)
{
thisOne = true;
theOtherOne = false;
hollow = false;
break;
}
if (form is theOtherOne)
{
theOtherOne = true;
thisOne = false;
hollow = false;
break;
}
}
}
我将与表单相关的布尔值设置为true,因此可以在其他函数中使用它们,如下所示:
I set the form-related booleans to true, so I can use them in another function, something like this:
private void ActionTime()
{
if (thisOne)
Debug.WriteLine("ThisOne is open");
if (theOtherOne)
Debug.WriteLine("The OtherOne is open");
}
ActionTime
是在ClickEvent上调用的,但操作永远不会发生...我想foreach-loop
ActionTime
is called on a ClickEvent, but the action never happens... I guess there goes something wrong in the foreach-loop
推荐答案
会出问题,请参见以下代码如果您的表单是Form1和Form2,则可以使用:
Please see this code which works if your forms are Form1 and Form2:
bool bForm1Open = false;
bool bForm2Open = false;
private void FormCheck()
{
bForm1Open = false;
bForm2Open = false;
foreach (Form form in Application.OpenForms)
{
if (form is Form1)
{
bForm1Open = true;
}
if (form is Form2)
{
bForm2Open = true;
}
}
}
private void ActionTime()
{
if (bForm1Open)
Debug.WriteLine("ThisOne is open");
if (bForm2Open)
Debug.WriteLine("The OtherOne is open");
}
这篇关于如何对BaseForm类中的特定表单执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!