Solved

Dynamic Buttons in c#

Posted on 2009-05-05
5
293 Views
Last Modified: 2012-05-06
Hello,

i need to change the background color of all my button.
Let say they are named button1, button2, etc.

I want to change the color in s for loop.
I attach a saple code to better explain.

Thank you very much
for (int n = 1; n < 60); n++)

            {

                

                button+n.BackColor = Color.Yellow;

                //obviussly is not working like that :)

            }

Open in new window

0
Comment
Question by:borntofly
5 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 250 total points
Comment Utility
to find a control by name you can

in asp.net you can use
Page.FindControl("Button" + i.ToString())

on a windows form
Controls["Button" + i.ToString()]
0
 

Author Closing Comment

by:borntofly
Comment Utility
Thank you very much
0
 
LVL 9

Expert Comment

by:tculler
Comment Utility
The above will only work if you set the Name property to the control. What I recommend is using a List of buttons instead. Each time you add a button to the form, add it to the Button List as well. For example:
System.Collections.Generic.List<System.Windows.Forms.Button> buttons

   = new System.Collections.Generic.List<System.Windows.Forms.Button>();
 

void AddButton(System.Windows.Forms.Button myButton)

{

     this.buttons.Add(myButton);

     this.Controls.Add(myButton);

}
 

void ChangeButtonsBackColors(System.Drawing.Color myColor)

{

     foreach(System.Windows.Forms.Button btn in this.buttons)

     {

          btn.BackColor = myColor;

     }

}

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
If you work with winforms.

The catchup here is that e.g. form.Controls[name] will return only controls that are on the form itself. If these buttons (or whatever) are placed on a panel or tabcontrol (or any other container) - they are not in this collection. You need some recursive function to get all buttons on a form if the form has nested container controls with buttons.

Another approach - create an array that references all the buttons, and iterate through the array:

Buitton[] buttons = {Button1, Button2,...}

foreach(Button b in buttons)
            {    
                b.BackColor = Color.Yellow;
            }
0
 
LVL 2

Expert Comment

by:SimonFerris
Comment Utility
To get each button on a form

foreach (var control in Controls)
            {
                if (typeof(Button) == control.GetType())
                {
                    ((Button)control).BackColor = Color.Yellow;
                }
            }
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now