c# - 如何为许多按钮分配颜色? [英] c# - How to Assign color to many buttons ?

查看:242
本文介绍了c# - 如何为许多按钮分配颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为backgroundcolor指定颜色 - forecolor - 15个按钮的boardercolor !!!



我的代码:(我使用标签)



Hi, i want to assign color to backgroundcolor - forecolor - boardercolor of 15 buttons !!!

my code : ( I Used Tag )

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 .

推荐答案

颜色不会改变,因为您实际创建了新按钮并为它们提供了颜色。您可以做什么:将按钮放在一个数组中,循环遍历数组并更改颜色:

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:
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;
}


好的,所以你要在循环中创建新的按钮对象。如果要为表单上的所有按钮设置颜色,则需要使用这些对象。这样的事情:



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:

foreach (Control control in Controls)
            {
                if (control is Button)
                {
                    // Set values here
                }
            }


这篇关于c# - 如何为许多按钮分配颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆