Dela via


Så här gör du: Skicka data till det aktiva MDI-barnet

Inom ramen för MDI-program (Multiple-Document Interface)måste du ofta skicka data till det aktiva underordnade fönstret, till exempel när användaren klistrar in data från Urklipp i ett MDI-program.

Anmärkning

Information om hur du verifierar vilket underordnat fönster som har fokus och skickar dess innehåll till Urklipp finns i Determining the Active MDI Child.

För att skicka data till det aktiva underordnade MDI-fönstret från Urklipp

  1. I en metod kopierar du texten i Urklipp till den aktiva kontrollen av det aktiva underordnade formuläret.

    Anmärkning

    Det här exemplet förutsätter att det finns ett överordnat MDI-formulär (Form1) som har ett eller flera underordnade MDI-fönster som innehåller en RichTextBox-kontroll. Mer information finns i Skapa MDI-huvudformulär.

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

Se även