Solved

Dynamic Buttons in c#

Posted on 2009-05-05
5
295 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
ID: 24308870
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
ID: 31578207
Thank you very much
0
 
LVL 9

Expert Comment

by:tculler
ID: 24309199
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
ID: 24309229
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
ID: 24309260
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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