Recorrer Controles de un TabControl

Para recorrer controles dentro de un formulario y asignar un evento a todos los controles podemos realizar de esta manera. De esta forma asignamos eventos a todos los controles cuando se activa (tiene foco) y cuando no esta activo (pierde el foco).

Simply hook Enter and Leave events - toggling the color in each. Save the last color saved in OnEnterto use in OnLeave
public Form1()
{
    InitializeComponent();

    var lastColorSaved = Color.Empty;

    foreach(Control child in this.Controls)
    {
        child.Enter += (s, e) =>
                            {
                                var control = (Control)s;
                                lastColorSaved = control.BackColor;
                                control.BackColor = Color.Red;
                            };
        child.Leave += (s, e) =>
                            {
                                ((Control)s).BackColor = lastColorSaved;
                            };
    }
}
Para hacer los mismo en un control TabControl, en este caso nuestro TabControl
se llama aTabControl.Buscamos la propiedad control 
    TabPage page = aTabControl.SelectedTab;

        var controls = page.Controls;

        foreach (var control in controls)
        {
            //do stuff
        }
Fuente: http://stackoverflow.com/questions/1744429/loop-through-controls-in-tabcontrol Fuente:http://stackoverflow.com/questions/9548217/changing-the-properties-of-the-active-control-automatically
Saludos
Adalberto Montanía

Comentarios