Files
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

4.0 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Determine the Active MDI Child 03/30/2017
csharp
vb
Clipboard [Windows Forms], copying data to
MDI [Windows Forms], child windows
child forms
MDI [Windows Forms], activating forms
MDI [Windows Forms], locating focus
33880ec3-0207-4c2b-a616-ff140443cc0f

How to: Determine the Active MDI Child

On occasion, you will want to provide a command that operates on the control that has focus on the currently active child form. For example, suppose you want to copy selected text from the child form's text box to the Clipboard. You would create a procedure that copies selected text to the Clipboard using the xref:System.Windows.Forms.Control.Click event of the Copy menu item on the standard Edit menu.

Because an MDI application can have many instances of the same child form, the procedure needs to know which form to use. To specify the correct form, use the xref:System.Windows.Forms.Form.ActiveMdiChild%2A property, which returns the child form that has the focus or that was most recently active.

When you have several controls on a form, you also need to specify which control is active. Like the xref:System.Windows.Forms.Form.ActiveMdiChild%2A property, the xref:System.Windows.Forms.ContainerControl.ActiveControl%2A property returns the control with the focus on the active child form. The procedure below illustrates a copy procedure that can be called from a child form menu, a menu on the MDI form, or a toolbar button.

To determine the active MDI child (to copy its text to the Clipboard)

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

    Note

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

    Public Sub mniCopy_Click(ByVal sender As Object, _  
       ByVal e As System.EventArgs) Handles mniCopy.Click  
    
       ' Determine the active child form.  
       Dim activeChild As Form = Me.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  
          Dim theBox As RichTextBox = _  
            TryCast(activeChild.ActiveControl, RichTextBox)  
    
          If (Not theBox Is Nothing) Then  
             'Put selected text on Clipboard.  
             Clipboard.SetDataObject(theBox.SelectedText)  
          Else  
             MessageBox.Show("You need to select a RichTextBox.")  
          End If  
       End If  
    End Sub  
    
    protected void mniCopy_Click (object sender, System.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)  
             {  
                // Put the selected text on the Clipboard.  
                Clipboard.SetDataObject(theBox.SelectedText);  
    
             }  
          }  
          catch  
          {  
             MessageBox.Show("You need to select a RichTextBox.");  
          }  
       }  
    }  
    

See also