Поделиться через


Практическое руководство. Отправка данных в активную дочернюю MDI-форму

Зачастую в контексте приложений многодокументного интерфейса (MDI) необходимо отправлять данные в активное дочернее окно, например при вставке пользователем данных из буфера обмена в приложение MDI.

Примечание.

Сведения о проверке фокуса дочернего окна и отправке его содержимого в буфер обмена см. в разделе Определение активного дочернего элемента MDI.

Отправка данных из буфера обмена в активное дочернее окно MDI

  1. В пределах метода скопируйте текст из буфера обмена в активный элемент управления активной дочерней формы.

    Примечание.

    В этом примере предполагается, что имеется родительская форма MDI (Form1) с одним или несколькими дочерними окнами MDI, содержащими элемент управления RichTextBox. Дополнительные сведения см. в разделе Создание родительских форм 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.");  
          }  
       }  
    }  
    

См. также