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!!!!
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:
For Each c As Control In Me.Controls
c.BackColor = Color.Red
Next c
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:
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)
If c.Controls.Count <> 0 Then
SetControlsBackColor(c.Controls, Backcolor)
End If
c.BackColor = Backcolor
Next c
End Sub
{
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:
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
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
{
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
//=======================================================