Condividi tramite


Procedura: inviare dati al figlio MDI attivo

Aggiornamento: novembre 2007

Spesso, nel contesto delle applicazioni MDI (Interfaccia a documenti multipli, Multiple Document Interface), è necessario inviare dati alla finestra figlio attiva, come quando l'utente incolla i dati dagli Appunti in un'applicazione MDI.

Nota:

Per informazioni sulla verifica della finestra figlio attiva e l'invio del relativo contenuto negli Appunti, vedere Determinazione del figlio MIDI attivo.

Per inviare i dati alla finestra figlio MDI attiva dagli Appunti

  • In un metodo, copiare il testo degli Appunti nel controllo attivo del form figlio attivo.

    Nota:

    Nell'esempio qui di seguito si presume l'esistenza di un form padre MDI (Form1) con una o più finestre figlio MDI contenenti un controllo RichTextBox. Per ulteriori informazioni, vedere Creazione di form padre 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.");
          }
       }
    }
    
    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.");
          }
       }
    }
    

Vedere anche

Attività

Procedura: creare form padre MDI

Procedura: creare form figlio MDI

Procedura: determinare il figlio MDI attivo

Procedura: disporre i form figlio MDI

Altre risorse

Applicazioni MDI (Interfaccia a documenti multipli, Multiple-Document Interface)