Ayuda con programa C#

Iniciado por frandy_Javier, 7 Febrero 2014, 18:36 PM

0 Miembros y 1 Visitante están viendo este tema.

frandy_Javier

Necesito crear un programa que contenga un boton para crear labels y botones entre otros componentes C# dependiendo de radio boton, sucede que he podido crear los botones con sus funcionalidades excepto el de cambiar color, debo crear un boton cambiar color que permita seleccionar todos los botones o label que existan en el form y les cambie el color.... Podria alguien ayudarme con una funcion para cambiar el color de todos los objetos creados en el form...

::) Gracias!!!!
Gracias, Frandy Javier :)

Eleкtro

#1
Hola,
las preguntas de C# van al subforo de C#.




Suponiendo que no necesitos recusión (controles dentro de paneles, etc...)

Método simple, no recursivo:

Código (vbnet) [Seleccionar]
       For Each c As Control In Me.Controls
           c.BackColor = Color.Red
       Next c


Código (csharp) [Seleccionar]
foreach (Control c in this.Controls) {
c.BackColor = Color.Red;
}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================



Método simple, recursivo:

Código (vbnet) [Seleccionar]
   Private Shadows Sub Shown() Handles MyBase.Shown

       SetControlsBackColor(Me.Controls, Color.Red)

   End Sub

   Private Sub SetControlsBackColor(ByVal Collection As Control.ControlCollection,
                                    ByVal Backcolor As Color)

       For Each c As Control In Collection

           If c.Controls.Count <> 0 Then
               SetControlsBackColor(c.Controls, Backcolor)
           End If

           c.BackColor = Backcolor

       Next c

   End Sub


Código (csharp) [Seleccionar]


private new void Shown()
{
SetControlsBackColor(this.Controls, Color.Red);

}


private void SetControlsBackColor(Control.ControlCollection Collection, Color Backcolor)
{

foreach (Control c in Collection) {
if (c.Controls.Count != 0) {
SetControlsBackColor(c.Controls, Backcolor);
}

c.BackColor = Backcolor;

}

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================




Método optimizado, no recursivo:

Código (vbnet) [Seleccionar]
Public Class Form1

   Private Shadows Sub Shown() Handles MyBase.Shown

       ControlIterator.PerformAction(Me.Controls, Sub(c As Control)
                                                      c.BackColor = Color.Green
                                                  End Sub)

   End Sub

   ' ControlIterator
   ' ( By Elektro )
   '
   ''' <summary>
   ''' Iterates a serie of Controls to perform an operation.
   ''' </summary>
   Public Class ControlIterator

       ''' <summary>
       ''' Perform an operation on all the Controls on the specified Control Collection.
       ''' </summary>
       ''' <param name="ControlCollection">Indicates the control collection where to find the controls.</param>
       ''' <param name="Operation">Indicates the Action to perform on the controls.</param>
       ''' <param name="ContainsName">Indicates that only controls containing name should be collected.</param>
       Public Shared Function PerformAction(ByVal ControlCollection As Control.ControlCollection,
                                            ByVal Operation As [Delegate],
                                            Optional ByVal ContainsName As String = Nothing) As Boolean

           Return PerformActionOnControls((From c As Object In ControlCollection), Operation, ContainsName)

       End Function

       ''' <summary>
       ''' Perform an operation on Controls.
       ''' </summary>
       Private Shared Function PerformActionOnControls(ByVal Controls As IEnumerable(Of Object),
                                                       ByVal Operation As [Delegate],
                                                       Optional ByVal ContainsName As String = Nothing) As Boolean

           If ContainsName IsNot Nothing Then
               Controls = Controls.Where(Function(ctrl) ctrl.name.contains(ContainsName))
               If Controls.Count = 0 Then Return False
           End If

           For Each [Control] As Object In Controls

               If [Control].InvokeRequired Then
                   [Control].Invoke(Operation, New Object() {[Control]})
               Else
                   Operation.Method.Invoke(Operation, New Object() {[Control]})
               End If

           Next [Control]

           Return True

       End Function

   End Class

End Class


Código (csharp) [Seleccionar]

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{


private new void Shown()
{
ControlIterator.PerformAction(this.Controls, (Control c) => { c.BackColor = Color.Green; });

}

// ControlIterator
// ( By Elektro )
//
/// <summary>
/// Iterates a serie of Controls to perform an operation.
/// </summary>
public class ControlIterator
{

/// <summary>
/// Perform an operation on all the Controls on the specified Control Collection.
/// </summary>
/// <param name="ControlCollection">Indicates the control collection where to find the controls.</param>
/// <param name="Operation">Indicates the Action to perform on the controls.</param>
/// <param name="ContainsName">Indicates that only controls containing name should be collected.</param>
public static bool PerformAction(Control.ControlCollection ControlCollection, Delegate Operation, string ContainsName = null)
{

return PerformActionOnControls((from c in ControlCollection), Operation, ContainsName);

}

/// <summary>
/// Perform an operation on Controls.
/// </summary>
private static bool PerformActionOnControls(IEnumerable<object> Controls, Delegate Operation, string ContainsName = null)
{

if (ContainsName != null) {
Controls = Controls.Where(ctrl => ctrl.name.contains(ContainsName));
if (Controls.Count == 0)
return false;
}


foreach (object Control in Controls) {
if (Control.InvokeRequired) {
Control.Invoke(Operation, new object[] { Control });
} else {
Operation.Method.Invoke(Operation, new object[] { Control });
}

}

return true;

}

}
public Form1()
{
Shown += Shown;
}

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================








frandy_Javier

Gracias, Frandy Javier :)