Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i want to assign color to backgroundcolor - forecolor - boardercolor of 15 buttons !!!

my code : ( I Used Tag )

C#
for (int i = 1; i < 16; i++)
            {
                Button button = new Button();
                button.Tag = i;

                button.BackColor = Properties.Settings.Default.cColorBack;
                button.FlatAppearance.BorderColor = Properties.Settings.Default.cColorFore;
                button.ForeColor = Properties.Settings.Default.cColor;
            }


but Colors of tagged buttons wont change , please help if you can .
Posted
Updated 22-Dec-14 21:17pm
v2
Comments
CPallini 23-Dec-14 3:12am    
What do you mean with "it doesn't work" ?
Member 11325242 23-Dec-14 3:15am    
Color of buttons not change
CPallini 23-Dec-14 3:27am    
You did not add the buttons to the form.
Praveen Kumar Upadhyay 23-Dec-14 3:17am    
If you want to change the conman settings of all buttons, you can do it in the design mode by selecting all the controls and then change it from property window.

If you want to change the things from the code, then your code looks OK. Can you please tell where you are using this loop code.
BillWoodruff 23-Dec-14 3:49am    
What Buttons have you already created at design-time: any ?

The colors don't change because you actually create new buttons and give them a color. What you can do instead: put the buttons in an array, loop over the array and change the color:
C#
Button[] buttons = new Button[] { button1, button2, button3, button4, ..., button15 }; // replace with the names of your buttons
for (int i = 0; i < buttons.Length; i++)
{
    Button currBtn = buttons[i];
    currBtn.BackColor = Properties.Settings.Default.cColorBack;
    currBtn.FlatAppearance.BorderColor = Properties.Settings.Default.cColorFore;
    currBtn.ForeColor = Properties.Settings.Default.cColor;
}
 
Share this answer
 
OK, so you are creating new button objects in the loop. If you want to set the colour for all the buttons on your form, you need to use those objects. Something like this:

C#
foreach (Control control in Controls)
            {
                if (control is Button)
                {
                    // Set values here
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900