Compartir a través de


Cómo: Determinar el formulario secundario MDI activo

Actualización: noviembre 2007

En determinadas ocasiones, deseará proporcionar un comando que actúe sobre el control que tiene el foco en el formulario secundario actualmente activo. Por ejemplo, suponga que desea copiar texto seleccionado desde el cuadro de texto del formulario secundario al Portapapeles. Deberá crear un procedimiento que copie el texto seleccionado al Portapapeles mediante el evento Click del elemento de menú Copiar del menú estándar Edición.

Dado que una aplicación MDI puede tener muchas instancias del mismo formulario secundario, el procedimiento necesita saber qué formulario debe utilizar. Para especificar el formulario correcto, utilice la propiedad ActiveMdiChild, que devuelve el formulario secundario que tiene el foco o el que estuvo activo más recientemente.

Cuando tenga varios controles en un formulario, deberá especificar también qué control está activo. Al igual que la propiedad ActiveMdiChild, la propiedad ActiveControl devuelve el control que tiene el foco en el formulario secundario activo. El procedimiento siguiente ilustra un procedimiento de copia, al que se puede llamar desde un menú de un formulario secundario, un menú del formulario MDI o un botón de la barra de herramientas.

Para determinar el formulario secundario MDI activo (a fin de copiar el texto que contiene en el Portapapeles)

  • Dentro de un método, copie en el Portapapeles el texto del control activo del formulario secundario activo.

    Nota:

    En este ejemplo se supone que hay un formulario primario MDI (Form1) que tiene una o varias ventanas secundarias MDI que, a su vez, contienen un control RichTextBox. Para obtener más información, vea Crear formularios primarios MDI.

    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
          Try
             Dim theBox As RichTextBox = _
             Ctype(activeChild.ActiveControl, RichTextBox)
             If (Not theBox Is Nothing) Then
                ' Put selected text on Clipboard.
                Clipboard.SetDataObject(theBox.SelectedText)
             End If
          Catch
             MessageBox.Show("You need to select a RichTextBox.")
          End Try
       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.");
          }
       }
    }
    
    private void menuItem5_Click(System.Object sender, System.EventArgs e) 
    {
       // Determine the active child form.
       Form activeChild = this.get_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.get_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.set_SelectedText(data.GetData(DataFormats.Text).ToString());
                }
             }
          }
          catch(System.Exception exp)
          {
    
             MessageBox.Show("You need to select a RichTextBox.");
          }
       }
    }
    

Vea también

Tareas

Cómo: Crear formularios principales MDI

Cómo: Crear formularios MDI secundarios

Cómo: Enviar datos al formulario secundario MDI activo

Cómo: Organizar formularios MDI secundarios

Otros recursos

Aplicaciones de interfaz de múltiples documentos (MDI)