Introduction
AS my previous article helps you to learn MDI Form and Child Form Creation. Now I am going to tell about the working of controls which has focus on the currently active child form.
Get to Work
With the help of below given example, you can learn how to create an procedure that copies selected text to the clipboard using the click event of the copy menu control. Here are the step required to build an procedure to copy selected text from the child form text box.
To Create a procedure on active form or giving a specific instance of the recently active child form you can use ActiveMdiChild property. Same as to specify the active control of the form on which procedure works you have use ActiveControl property.
Step to Create and Implement MDI Child Form
-
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.
-
Add one More control in Main form MenuStrip As Copy under Edit Menu.
-
Double click on Copy control and write this Code.
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
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.");
}
}
}
-
Debug the application and click on New button a MDI Child form with RichTextBox will open. Now you write anything in text box and select them now go to Copy control in Edit Menu and Click on it. Your text was copied you can past the text from your clipboard.
Summary
In this article, we discussed how to Create a procedure which helps to copy selected text from the active child form to clipboard using Visual Studio 2010 and C#.