elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Ayuda con programa C#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda con programa C#  (Leído 1,577 veces)
frandy_Javier

Desconectado Desconectado

Mensajes: 2



Ver Perfil
Ayuda con programa C#
« en: 7 Febrero 2014, 18:36 pm »

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!!!!


En línea

Gracias, Frandy Javier :)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: Ayuda con programa C#
« Respuesta #1 en: 7 Febrero 2014, 18:56 pm »

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
  1.        For Each c As Control In Me.Controls
  2.            c.BackColor = Color.Red
  3.        Next c

Código
  1. foreach (Control c in this.Controls) {
  2. c.BackColor = Color.Red;
  3. }
  4.  
  5. //=======================================================
  6. //Service provided by Telerik (www.telerik.com)
  7. //Conversion powered by NRefactory.
  8. //Twitter: @telerik
  9. //Facebook: facebook.com/telerik
  10. //=======================================================


Método simple, recursivo:

Código
  1.    Private Shadows Sub Shown() Handles MyBase.Shown
  2.  
  3.        SetControlsBackColor(Me.Controls, Color.Red)
  4.  
  5.    End Sub
  6.  
  7.    Private Sub SetControlsBackColor(ByVal Collection As Control.ControlCollection,
  8.                                     ByVal Backcolor As Color)
  9.  
  10.        For Each c As Control In Collection
  11.  
  12.            If c.Controls.Count <> 0 Then
  13.                SetControlsBackColor(c.Controls, Backcolor)
  14.            End If
  15.  
  16.            c.BackColor = Backcolor
  17.  
  18.        Next c
  19.  
  20.    End Sub

Código
  1.  
  2. private new void Shown()
  3. {
  4. SetControlsBackColor(this.Controls, Color.Red);
  5.  
  6. }
  7.  
  8.  
  9. private void SetControlsBackColor(Control.ControlCollection Collection, Color Backcolor)
  10. {
  11.  
  12. foreach (Control c in Collection) {
  13. if (c.Controls.Count != 0) {
  14. SetControlsBackColor(c.Controls, Backcolor);
  15. }
  16.  
  17. c.BackColor = Backcolor;
  18.  
  19. }
  20.  
  21. }
  22.  
  23. //=======================================================
  24. //Service provided by Telerik (www.telerik.com)
  25. //Conversion powered by NRefactory.
  26. //Twitter: @telerik
  27. //Facebook: facebook.com/telerik
  28. //=======================================================



Método optimizado, no recursivo:

Código
  1. Public Class Form1
  2.  
  3.    Private Shadows Sub Shown() Handles MyBase.Shown
  4.  
  5.        ControlIterator.PerformAction(Me.Controls, Sub(c As Control)
  6.                                                       c.BackColor = Color.Green
  7.                                                   End Sub)
  8.  
  9.    End Sub
  10.  
  11.    ' ControlIterator
  12.    ' ( By Elektro )
  13.    '
  14.    ''' <summary>
  15.    ''' Iterates a serie of Controls to perform an operation.
  16.    ''' </summary>
  17.    Public Class ControlIterator
  18.  
  19.        ''' <summary>
  20.        ''' Perform an operation on all the Controls on the specified Control Collection.
  21.        ''' </summary>
  22.        ''' <param name="ControlCollection">Indicates the control collection where to find the controls.</param>
  23.        ''' <param name="Operation">Indicates the Action to perform on the controls.</param>
  24.        ''' <param name="ContainsName">Indicates that only controls containing name should be collected.</param>
  25.        Public Shared Function PerformAction(ByVal ControlCollection As Control.ControlCollection,
  26.                                             ByVal Operation As [Delegate],
  27.                                             Optional ByVal ContainsName As String = Nothing) As Boolean
  28.  
  29.            Return PerformActionOnControls((From c As Object In ControlCollection), Operation, ContainsName)
  30.  
  31.        End Function
  32.  
  33.        ''' <summary>
  34.        ''' Perform an operation on Controls.
  35.        ''' </summary>
  36.        Private Shared Function PerformActionOnControls(ByVal Controls As IEnumerable(Of Object),
  37.                                                        ByVal Operation As [Delegate],
  38.                                                        Optional ByVal ContainsName As String = Nothing) As Boolean
  39.  
  40.            If ContainsName IsNot Nothing Then
  41.                Controls = Controls.Where(Function(ctrl) ctrl.name.contains(ContainsName))
  42.                If Controls.Count = 0 Then Return False
  43.            End If
  44.  
  45.            For Each [Control] As Object In Controls
  46.  
  47.                If [Control].InvokeRequired Then
  48.                    [Control].Invoke(Operation, New Object() {[Control]})
  49.                Else
  50.                    Operation.Method.Invoke(Operation, New Object() {[Control]})
  51.                End If
  52.  
  53.            Next [Control]
  54.  
  55.            Return True
  56.  
  57.        End Function
  58.  
  59.    End Class
  60.  
  61. End Class

Código
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. public class Form1
  8. {
  9.  
  10.  
  11. private new void Shown()
  12. {
  13. ControlIterator.PerformAction(this.Controls, (Control c) => { c.BackColor = Color.Green; });
  14.  
  15. }
  16.  
  17. // ControlIterator
  18. // ( By Elektro )
  19. //
  20. /// <summary>
  21. /// Iterates a serie of Controls to perform an operation.
  22. /// </summary>
  23. public class ControlIterator
  24. {
  25.  
  26. /// <summary>
  27. /// Perform an operation on all the Controls on the specified Control Collection.
  28. /// </summary>
  29. /// <param name="ControlCollection">Indicates the control collection where to find the controls.</param>
  30. /// <param name="Operation">Indicates the Action to perform on the controls.</param>
  31. /// <param name="ContainsName">Indicates that only controls containing name should be collected.</param>
  32. public static bool PerformAction(Control.ControlCollection ControlCollection, Delegate Operation, string ContainsName = null)
  33. {
  34.  
  35. return PerformActionOnControls((from c in ControlCollection), Operation, ContainsName);
  36.  
  37. }
  38.  
  39. /// <summary>
  40. /// Perform an operation on Controls.
  41. /// </summary>
  42. private static bool PerformActionOnControls(IEnumerable<object> Controls, Delegate Operation, string ContainsName = null)
  43. {
  44.  
  45. if (ContainsName != null) {
  46. Controls = Controls.Where(ctrl => ctrl.name.contains(ContainsName));
  47. if (Controls.Count == 0)
  48. return false;
  49. }
  50.  
  51.  
  52. foreach (object Control in Controls) {
  53. if (Control.InvokeRequired) {
  54. Control.Invoke(Operation, new object[] { Control });
  55. } else {
  56. Operation.Method.Invoke(Operation, new object[] { Control });
  57. }
  58.  
  59. }
  60.  
  61. return true;
  62.  
  63. }
  64.  
  65. }
  66. public Form1()
  67. {
  68. Shown += Shown;
  69. }
  70.  
  71. }
  72.  
  73. //=======================================================
  74. //Service provided by Telerik (www.telerik.com)
  75. //Conversion powered by NRefactory.
  76. //Twitter: @telerik
  77. //Facebook: facebook.com/telerik
  78. //=======================================================
  79.  


« Última modificación: 7 Febrero 2014, 19:07 pm por Eleкtro » En línea

frandy_Javier

Desconectado Desconectado

Mensajes: 2



Ver Perfil
Re: Ayuda con programa C#
« Respuesta #2 en: 7 Febrero 2014, 20:13 pm »

gracias :)
En línea

Gracias, Frandy Javier :)
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
AYUDA CON PROGRAMA
Diseño Gráfico
Luisango 1 2,415 Último mensaje 17 Abril 2005, 13:25 pm
por Sub_Cero
ayuda con mi programa
Programación Visual Basic
nitrox 2 2,314 Último mensaje 31 Julio 2005, 00:48 am
por Slasher-K
ayuda programa!!
Programación C/C++
rodrigo_103 4 3,326 Último mensaje 16 Septiembre 2012, 21:58 pm
por rodrigo_103
[Ayuda] ¿Cómo ejecutar otro programa desde mi programa de C#?
.NET (C#, VB.NET, ASP)
Zodiak98 1 6,019 Último mensaje 8 Diciembre 2013, 01:51 am
por Eleкtro
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines