Export (0) Print
Expand All

How to: Send Data to the Active MDI Child

Often, within the context of Multiple-Document Interface (MDI) Applications, you will need to send data to the active child window, such as when the user pastes data from the Clipboard into an MDI application.

Note Note

For information about verifying which child window has focus and sending its contents to the Clipboard, see Determining the Active MDI Child.

To send data to the active MDI child window from the Clipboard

  • Within a method, copy the text on the Clipboard to the active control of the active child form.

    Note Note

    This example assumes there is an MDI parent form (Form1) that has one or more MDI child windows containing a RichTextBox control. For more information, see Creating MDI Parent Forms.

    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.");
          }
       }
    }
    
Show:
© 2014 Microsoft