Comment : envoyer des données à l'enfant MDI actif

Il vous arrivera souvent, dans le contexte des applications d'interface multidocument (MDI, Multiple Document Interface), de devoir envoyer des données à la fenêtre enfant active, par exemple pour coller dans une application MDI les données que l'utilisateur a placées dans le Presse-papiers.

Notes

Pour plus d'informations sur la façon de déterminer quelle fenêtre enfant a le focus et envoyer son contenu au Presse-papiers, consultez Détermination de l'enfant MDI actif.

Pour envoyer des données à la fenêtre MDI enfant active à partir du Presse-papiers

  • Dans une méthode, copiez le texte du Presse-papiers dans le contrôle actif du formulaire enfant actif.

    Notes

    Cet exemple suppose qu'il existe un formulaire MDI parent (Form1) doté d'une ou de plusieurs fenêtres MDI enfants contenant un contrôle RichTextBox. Pour plus d'informations, consultez Création de formulaires MDI parents.

    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.");
          }
       }
    }
    
    private void mniPaste_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  ) 
             {
                // Put the selected text on the Clipboard.
                Clipboard.SetDataObject(theBox.get_SelectedText());
             }
          } 
          catch(System.Exception exp)
          {
             MessageBox.Show("You need to select a RichTextBox.");
          }
       }
    }
    

Voir aussi

Tâches

Comment : créer des formulaires MDI parents
Comment : créer des formulaires MDI enfants
Comment : déterminer l'enfant MDI actif
Comment : réorganiser des formulaires MDI enfants

Autres ressources

Applications d'interface multidocument (MDI, Multiple Document Interface)