本文介绍了如何确定活动表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

有没有人知道如何(通常)使用静态确定应用程序的当前活动表单?功能(所以我可以从任何地方调用它)。我找不到任何有条不紊的方式,所以我为初学者写了以下内容:


public static Form GetActiveForm()

{

//为MDI应用程序返回null

表单activeForm = Form.ActiveForm;

if(activeForm == null)

{

FormCollection openForms = Application.OpenForms;

for(int i = 0; i< openForms.Count&& activeForm == null ; ++ i)

{

表格openForm = openForms [i];

if(openForm.IsMdiContainer)

{

activeForm = openForm.ActiveMdiChild;

}

}

}


返回activeForm;

}


但是,如果在调用此方法时没有打开MDI子窗体,则应返回MDI父窗体。如果应用程序中有多个MDI父项,那该怎么办(不太可能,但我可以假设)。那么你如何确定哪个是活跃的。在任何情况下,上面的代码看起来像是黑客。对我而言,我担心我可能会忽视的任何问题。事实上,整个情况似乎是对MSFT部分的一次重大监督,所以我对此感到不安。如果有人能让我走上正确的轨道,我会很感激。在此先感谢。

Hi there,

Does anyone know how to (generically) determine the currently active form for an application using a "static" function (so I can call it from anywhere). There is no offiical way I''ve been able to find so I''ve written the following for starters:

public static Form GetActiveForm()
{
// Returns null for an MDI app
Form activeForm = Form.ActiveForm;
if (activeForm == null)
{
FormCollection openForms = Application.OpenForms;
for (int i= 0; i < openForms.Count && activeForm == null; ++i)
{
Form openForm = openForms[i];
if (openForm.IsMdiContainer)
{
activeForm = openForm.ActiveMdiChild;
}
}
}

return activeForm;
}

However, if no MDI child forms are open when this is called then the MDI parent should be returned. What if there''s more than one MDI parent in the application however (unlikely but possible I assume). How do you then determine which is active. In any case, the code above seems like a "hack" to me and I''m concerned about any issues I may be neglecting. In fact, the entire situation seems like a major oversight on MSFT''s part so I''m uneasy about doing this. If someone can therefore set me on the right track I''d appreciate it. Thanks in advance.

推荐答案




如果是MDI应用程序,那么关于返回null的评论会让我感到困惑,因为这不是我注意到的行为。在MDI应用程序中,活动表单

通常是MDI容器,除非显示对话框。

如果你想要,那就是你如何得到它。如果你想要活跃的

孩子,那么测试一下,看看ActiveForm是否是一个MDI容器,如果是这样,那么

调用ActiveMdiChild。您只需要执行以下操作:


表格activeForm = Form.ActiveForm;


if(activeForm.IsMdiContainer&& activeForm .ActiveMdiChild!= null)

{

activeForm = activeForm.ActiveMdiChild;

}

-

Tom Porterfield

Your comment about returning null if it is an MDI app confuses me as
that is not the behavior I have noticed. In an MDI app the active form
will usually be the MDI container unless a dialog is being displayed.
If you want that, then that''s how you get it. If you want the active
child then test to see if the ActiveForm is an MDI container and if so
call ActiveMdiChild. You should only need to do the following:

Form activeForm = Form.ActiveForm;

if (activeForm.IsMdiContainer && activeForm.ActiveMdiChild != null)
{
activeForm = activeForm.ActiveMdiChild;
}
--
Tom Porterfield




感谢你和Tom我解决了这个问题。我的应用程序实际上是一个MDI应用程序

和ActiveForm属性总是返回null所以我只是假设

这是MDI应用程序中的正常行为。也就是说,我认为我不得不依赖

ActiveMdiChild。相反,因此需要在我的

原始帖子中使用该功能。您的回答似乎表明这种行为并不正常

但是我尝试清除主要表单''IsMdiContainer"财产,但

ActiveForm财产仍然是空的。有些东西不在这儿

显然。事实证明,ActiveForm可以说是ActiveForm。

所有的财产都不是空的。如果在断点处于活动状态时进行测试,则它仅为null。 VS

调试器总是为此属性IOW返回null,但如果你指定

" ActiveForm"在点击断点之前变量,现在变量

显示主要形式。愚弄我不知道。并且在MSFT上感到遗憾

首先说明这是设计但是没有解决他们已经知道18个月的明显问题(和

令人困惑)(见这里


无论如何,再次感谢你们两位。

Thanks to both you and Tom I solved the issue. My app is actually an MDI app
and the "ActiveForm" property was always returning null so I just assumed
this was normal behaviour in an MDI app. That is, I assumed I had to rely on
the "ActiveMdiChild" property instead, hence the need for the function in my
original post. Your responses seemed to indicate this behaviour isn''t normal
however so I tried clearing the main form''s "IsMdiContainer" property but
the "ActiveForm" property was still null. Something wasn''t right here
obviously. Well it turns out that the "ActiveForm" property wasn''t null at
all. It''s only null if you test it while a breakpoint is active. The VS
debugger always returns null for this property IOW but if you assign
"ActiveForm" to a variable before hitting the breakpoint, the variable now
corrctly shows the main form. Foolish me for not knowing. And shame on MSFT
for first stating this was by "design" but not fixing an obvious (and
confusing) problem they''ve known about for 18 months now (see here
http://connect.microsoft.com/VisualS...backID=111915).
Anyway, thanks again to both of you.


这篇关于如何确定活动表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:33