我在load里面生成了一个按钮,但我想timer控件时间一到就改变着按钮的颜色,因为有几个按钮,我就定义了        
Button[] LButton = new Button[3]; 
然后再timer里面用switch来选择按钮的颜色,为何时间不了呢?
另外我之前还采用了
List<ButtonBase> LButton = new List<ButtonBase>();用这个调用也改变不了颜色,那究竟该如何做呢?

解决方案 »

  1.   

    timer  控件 是一个新的线程吧。   跨线程操作 要使用委托
      

  2.   

    每个按钮创建后,添加:
    button1.UseVisualStyleBackColor = false;
    这样,表面颜色可该。
    但边缘很难看。最好是从Button类继承自己的按钮类,自行控制其绘制
      

  3.   

    namespace changeButtonColor
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            ChangeButtonColor theChangeColor1;
            ChangeButtonColor theChangeColor2;
            ChangeButtonColor theChangeColor3;
            private void Form1_Load(object sender, EventArgs e)
            {
                Button[] LButton = new Button[3];
                LButton[0] = new Button();
                LButton[1] = new Button();
                LButton[2] = new Button();
                LButton[0].Location = new Point(100, 200);
                LButton[1].Location = new Point(200, 200);
                LButton[2].Location = new Point(300, 200);
                this.Controls.Add(LButton[0]);
                this.Controls.Add(LButton[1]);
                this.Controls.Add(LButton[2]);
                theChangeColor1 = new ChangeButtonColor(LButton[0]);
                theChangeColor2 = new ChangeButtonColor(LButton[1]);
                theChangeColor3 = new ChangeButtonColor(LButton[2]);
                timer1.Start();
            }        private class ChangeButtonColor
            {
                static int ChangeTime = 0;
                private Button theButton;            public ChangeButtonColor(Button LButton)
                {
                    this.theButton = LButton;
                }            public void changeColor()
                {
                    if (ChangeTime > int.MaxValue - 1)
                    {
                        ChangeTime = 0;
                    }                if (ChangeTime % 2 == 0)
                    {
                        theButton.BackColor = Color.Black;
                    }
                    else
                    {
                        this.theButton.BackColor = Color.Red;
                    }                ChangeTime += 1;
                }        }        private void timer1_Tick(object sender, EventArgs e)
            {
                theChangeColor1.changeColor();
                theChangeColor2.changeColor();
                theChangeColor3.changeColor();
            }
        }
    }
      

  4.   

    那只是个实现用timer来改变按钮颜色的一个示例,方法有很多