Working With Active MDI Child Form Controls: Part 2


Introduction

AS my previous article helps you to learn Create procedure for coping text from RichTextBox To Clipboard. Now I am going to tell about the working of Pest controls by which you can Pest your Clipboard Copied text into MDI Child form RichTextBox. 

Get to Work

With the help of below given example, you can learn how to create an procedure that Pest your copied text to the RichTextBox using the click event of the Pest menu control. Here are the step required to build an procedure to pest text from Clipboard to MDI child form RichTextBox.

Step to Create and Implement MDI Child Form
  1. Assumes there is an MDI parent form having MenuStrip with option New, Window and Close in New menu, main form contain one Child form having a RichTexBox. For Details, see Creating an MDI Form.
     
    1.gif

  2. Add one More control in Main form MenuStrip As Pest under Edit Menu.

     3.1.gif
     
  3. Double click on Pest control and write this Code.

        private void pestToolStripMenuItem_Click(object sender, 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)
                    {
                        // 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.");
                }
            }
        }

     
  4. Debug the application and click on New button a MDI Child form with RichTextBox will open. Now by using Pest control in the Edit Menu you can pest your copied text into the RichTextBox.

     3.2.gif
Summary

In this article, we discussed how to Create a procedure which helps to pest the text from clipboard to MDI Child form RichTextBox using Visual Studio 2010 and C#.

Up Next
    Ebook Download
    View all
    Learn
    View all