방법: 활성 MDI 자식으로 데이터 보내기

MDI(다중 문서 인터페이스) 애플리케이션의 컨텍스트 내에서 사용자가 클립보드의 데이터를 MDI 애플리케이션에 붙여넣을 때와 같이 활성 자식 창으로 데이터를 보내야 하는 경우가 많습니다.

참고

포커스가 있는 자식 창을 확인하고 클립보드로 콘텐츠를 보내는 방법에 대한 자세한 내용은 활성 MDI 자식 확인을 참조하세요.

클립보드에서 활성 MDI 자식 창으로 데이터를 보내려면 다음을 수행합니다.

  1. 메서드 내에서 클립보드의 텍스트를 활성 자식 양식의 활성 컨트롤에 복사합니다.

    참고

    이 예제에서는 RichTextBox 컨트롤을 포함하는 하나 이상의 MDI 자식 창이 있는 MDI 부모 양식(Form1)이 있다고 가정합니다. 자세한 내용은 MDI 부모 양식 만들기를 참조하세요.

    Public Sub mniPaste_Click(ByVal sender As Object, _  
       ByVal e As System.EventArgs) Handles mniPaste.Click  
    
       ' Determine the active child form.  
       Dim activeChild As Form = Me.ParentForm.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  
                ' Create a new instance of the DataObject interface.  
                Dim data As IDataObject = 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)) Then  
                   theBox.SelectedText = data.GetData(DataFormats.Text).ToString()  
                End If  
             End If  
          Catch  
             MessageBox.Show("You need to select a RichTextBox.")  
          End Try  
       End If  
    End Sub  
    
    protected void mniPaste_Click (object sender, System.EventArgs e)  
    {  
      // Determine the active child form.  
       Form activeChild = this.ParentForm.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)  
             {  
                // 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.SelectedText = data.GetData(DataFormats.Text).ToString();
                }  
             }  
          }  
          catch
          {  
             MessageBox.Show("You need to select a RichTextBox.");  
          }  
       }  
    }  
    

참고 항목