Comment : déterminer l'enfant MDI actif

Mise à jour : novembre 2007

Vous aurez parfois besoin de fournir une commande opérant sur le contrôle qui a le focus dans le formulaire enfant actif. Supposons, par exemple, que vous voulez copier un texte sélectionné dans la zone de texte du formulaire enfant vers le Presse-papiers. Pour ce faire, vous devez créer une procédure copiant le texte sélectionné dans le Presse-papiers à l'aide de l'événement Click de l'élément Copier du menu Edition standard.

Étant donné qu'une application MDI peut posséder plusieurs instances du même formulaire enfant, la procédure doit savoir quel formulaire utiliser. Pour spécifier le formulaire à utiliser, faites appel à la propriété ActiveMdiChild, laquelle retourne le formulaire enfant qui a le focus ou qui était actif en dernier.

Lorsqu'il existe plusieurs contrôles dans un formulaire, vous devez également spécifier lequel est actif. À l'instar de la propriété ActiveMdiChild, la propriété ActiveControl retourne le contrôle qui a le focus dans le formulaire enfant actif. La procédure ci-dessous montre une procédure de copie pouvant être appelée à partir d'un menu de formulaire enfant, un menu de formulaire MDI ou un bouton de la barre d'outils.

Pour déterminer l'enfant MDI actif (et copier son texte dans le Presse-papiers)

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

    Remarque :

    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 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.");
          }
       }
    }
    

Voir aussi

Tâches

Comment : créer des formulaires MDI parents

Comment : créer des formulaires MDI enfants

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

Comment : réorganiser des formulaires MDI enfants

Autres ressources

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