日期:2014-05-20  浏览次数:21048 次

在MDI主窗体中怎样得到当前的焦点子窗体?
同上

------解决方案--------------------
Form.ActiveMdiChild 属性
------解决方案--------------------
属性值
返回表示当前活动的 MDI 子窗口的 Form,或者如果当前没有子窗口,则返回空引用(在 Visual Basic 中为 Nothing)。
备注
可使用此方法确定 MDI 应用程序中是否有任何打开的 MDI 子窗体。也可使用此方法从 MDI 子窗口的 MDI 父窗体或者从应用程序中显示的其他窗体对该 MDI 子窗口执行操作。

如果当前活动窗体不是 MDI 子窗体,则可使用 ActiveForm 属性获得对它的引用。

------解决方案--------------------
示例
下面的代码示例获取对活动 MDI 子窗体的引用,并依次通过该窗体上的所有 TextBox 控件,重置这些控件的 Text 属性。此示例要求已创建 MDI 父窗体,而且从 MDI 父窗体执行此方法调用。

public void ClearAllChildFormText()
{
// Obtain a reference to the currently active MDI child form.
Form tempChild = this.ActiveMdiChild;

// Loop through all controls on the child form.
for (int i = 0; i < tempChild.Controls.Count; i++)
{
// Determine if the current control on the child form is a TextBox.
if (tempChild.Controls[i] is TextBox)
{
// Clear the contents of the control since it is a TextBox.
tempChild.Controls[i].Text = " ";
}
}
}