Share via


HOW TO:決定作用中的 MDI 子系

更新:2007 年 11 月

有時,您會想要提供一個在控制項執行的命令,而此控制項在目前作用中的子表單上取得焦點 (Focus)。例如,假設您想要從子表單的文字方塊複製選取文字到剪貼簿。您將會建立一個程序,使用標準 [編輯] 功能表中 [複製] 功能表項目的 Click 事件,將選取文字複製到剪貼簿。

由於 MDI 應用程式對於同一子表單可以有許多執行個體,因此程序需要知道應使用的表單是哪一個。若要指定正確的表單,請使用 ActiveMdiChild 屬性,傳回取得焦點或最近變成作用中的子表單。

如果表單上有數個控制項,您也需要指定作用中的控制項是哪一個。ActiveControl 屬性與 ActiveMdiChild 屬性類似,都會傳回在作用中子表單上取得焦點的控制項。下列程序說明可從子表單功能表、MDI 表單中的功能表或工具列按鈕呼叫的複製程序。

若要決定作用中的 MDI 子系 (複製其文字至剪貼簿)

  • 在一個方法中,將作用中子表單的作用中控制項文字複製到剪貼簿。

    注意事項:

    本範例假設 MDI 父表單 (Form1) 具有一或多個包含 RichTextBox 控制項的 MDI 子視窗。如需詳細資訊,請參閱建立 MDI 父表單

    Public Sub mniCopy_Click(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles mniCopy.Click
    
       ' Determine the active child form.
       Dim activeChild As Form = Me.ActiveMDIChild
    
       ' If there is an active child form, find the active control, which
       ' in this example should be a RichTextBox.
       If (Not activeChild Is Nothing) Then
          Try
             Dim theBox As RichTextBox = _
             Ctype(activeChild.ActiveControl, RichTextBox)
             If (Not theBox Is Nothing) Then
                ' Put selected text on Clipboard.
                Clipboard.SetDataObject(theBox.SelectedText)
             End If
          Catch
             MessageBox.Show("You need to select a RichTextBox.")
          End Try
       End If
    End Sub
    
    protected void mniCopy_Click (object sender, System.EventArgs e)
    {
       // Determine the active child form.
       Form activeChild = this.ActiveMdiChild;
    
       // If there is an active child form, find the active control, which
       // in this example should be a RichTextBox.
       if (activeChild != null)
       {  
          try
          {
             RichTextBox theBox = (RichTextBox)activeChild.ActiveControl;
             if (theBox != null)
             {
                // Put the selected text on the Clipboard.
                Clipboard.SetDataObject(theBox.SelectedText);
    
             }
          }
          catch
          {
             MessageBox.Show("You need to select a RichTextBox.");
          }
       }
    }
    
    private void menuItem5_Click(System.Object sender, System.EventArgs e) 
    {
       // Determine the active child form.
       Form activeChild = this.get_ActiveMdiChild();
    
       // If there is an active child form, find the active control, which
       // in this example should be a RichTextBox.
       if ( activeChild  != null  ) 
       {
          try 
          {
             RichTextBox theBox = ((RichTextBox)(activeChild.get_ActiveControl()));
             if ( theBox  != null  ) 
             {
                // Create a new instance of the DataObject interface.
                IDataObject data = Clipboard.GetDataObject();
                // If the data is text, then set the text of the 
                // RichTextBox to the text in the clipboard.
                if (data.GetDataPresent(DataFormats.Text)) 
                {
                   theBox.set_SelectedText(data.GetData(DataFormats.Text).ToString());
                }
             }
          }
          catch(System.Exception exp)
          {
    
             MessageBox.Show("You need to select a RichTextBox.");
          }
       }
    }
    

請參閱

工作

HOW TO:建立 MDI 父表單

HOW TO:建立 MDI 子表單

HOW To:傳送資料至作用中的 MDI 子系

HOW TO:安排 MDI 子表單

其他資源

多重文件介面 (MDI) 應用程式