如题...
现有一个MDI主窗体,主窗体上有工具栏
打开了若干子窗体
请问如何知道现在选中的子窗体是哪一个
并且点击主窗体的工具栏某一按钮(比如说加背景色)
然后选中的子窗体就会有背景色?

解决方案 »

  1.   

    Form.ActiveMdiChild:返回表示当前活动的 MDI 子窗口的 Form,或者如果当前没有子窗口,则返回 nullNothingnullptrnull 引用。可使用此方法确定 MDI 应用程序中是否有任何打开的 MDI 子窗体。也可使用此方法从 MDI 子窗口的 MDI 父窗体或者从应用程序中显示的其他窗体对该 MDI 子窗口执行操作。如果当前活动窗体不是 MDI 子窗体,则可使用 ActiveForm 属性获得对它的引用。
      

  2.   

    下面的代码示例获取对活动 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 = "";
           }
        }
     }