if (this.Button1.BackColor == Color.Lime)
            {
                this.Button1.BackColor = Color.Red;
                return;
            }
            if (this.Button1.BackColor == Color.Red)
            {
                this.Button1.BackColor = Color.Yellow;
                return;
            }
            if (this.Button1.BackColor == Color.Yellow)
            {
                this.Button1.BackColor = SystemColors.Control;
                return;
            }
如何可以一直点按钮一直循环~比如点一下红色,再点一下黄色,再点默认色,一直循环下去

解决方案 »

  1.   

    Color[] colors = new Color[] { Color.Red, Color.Yellow, ....} // colors that you want to set in the loop.int currentIndex = colors.IndexOf(Button1.BackColor);
    currentIndex = (currentIndex + 1 == colors.Length || currentIndex < 0) ? 0 : currentIndex + 1;
    Button1.BackColor = colors[currentIndex];I didnot test the above code. but that's question is easy. you are weak. need to study hard.
      

  2.   

    按照你的写法,把头尾接起来就行了:
    if (this.Button1.BackColor == SystemColors.Control) 
        this.Button1.BackColor = Color.Red; 
    else if (this.Button1.BackColor == Color.Red) 
        this.Button1.BackColor = Color.Yellow; 
    else
        this.Button1.BackColor = SystemColors.Control; 
      

  3.   

    你想复杂了 我也遇见过一样的问题  不过其实代码很简单
            if (this.Button1.BackColor == Color.Lime) 
           { 
              this.Button1.BackColor = Color.Red; 
                    
           }else
           {
             this.Button1.BackColor == Color.Lime
           }
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Drawing;namespace WebApplication1 {
        public partial class _Default : System.Web.UI.Page {
            private Queue<Color> list = new Queue<Color>();
            protected void Page_Load( object sender, EventArgs e ) {
                if( !IsPostBack ) {
                    if(list.Count <= 0 ) {
                        list.Enqueue( Color.Yellow );
                        list.Enqueue( Color.Blue );
                        list.Enqueue( Color.Red );
                        ViewState[ "color" ] = list;
                    }
                }        }        protected void Button1_Click( object sender, EventArgs e ) {
                Queue<Color> list = LoopColor;
                Button1.BackColor = list.Peek();
                Response.Write( Button1.BackColor.ToString() );
                list.Enqueue( list.Dequeue() );
            }
            public Queue<Color> LoopColor {
                get {
                    return ViewState[ "color" ] as Queue<Color>;
                }
                set {
                    ViewState[ "color" ] = value;
                }
            }
        }
    }
      

  5.   


    int currentIndex = colors.IndexOf(Button1.BackColor); 
    currentIndex = (currentIndex + 1) / colors.Length; 
    Button1.BackColor = colors[currentIndex]; 
      

  6.   

    不好意思写错了
    int currentIndex = colors.IndexOf(Button1.BackColor); 
    currentIndex = (currentIndex + 1) colors.Length; 
    Button1.BackColor = colors[currentIndex]; 
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Drawing;namespace WebApplication1 {
        public partial class _Default : System.Web.UI.Page {
            private Queue<Color> list = new Queue<Color>();
            protected void Page_Load( object sender, EventArgs e ) {
                if( !IsPostBack ) {
                    if(list.Count <= 0 ) {
                        list.Enqueue( Color.Yellow );
                        list.Enqueue( Color.Blue );
                        list.Enqueue( Color.Red );
                        list.Enqueue( Color.Aqua );
                        list.Enqueue( Color.Brown );
                        list.Enqueue( Color.DarkGoldenrod );
                        list.Enqueue( Color.Ivory );
                        //有多少都按照上述格式添加即可,
                        ViewState[ "color" ] = list;
                    }
                }        }        protected void Button1_Click( object sender, EventArgs e ) {
                Queue<Color> list = LoopColor;
                Button1.BackColor = list.Peek();
                Response.Write( Button1.BackColor.ToString() );
                list.Enqueue( list.Dequeue() );
            }
            public Queue<Color> LoopColor {
                get {
                    return ViewState[ "color" ] as Queue<Color>;
                }
                set {
                    ViewState[ "color" ] = value;
                }
            }
        }
    }不知道为何不使用JS的方式,这种页面上的逻辑交给客户端操作似乎更合理些!完全没必要上服务器上转个圈
      

  8.   

                if (this.button6.BackColor == SystemColors.Control)
                {
                    this.button6.BackColor = Color.Red;
                    return;
                }
                if (this.button6.BackColor == Color.Red)
                {
                    this.button6.BackColor = Color.Yellow;
                    return;
                }
                if (this.button6.BackColor == Color.Yellow)
                {
                    this.button6.BackColor =SystemColors.Control  ;
                    return;
                } 
      

  9.   

    C#可以的~JS脚本不行~按钮点上去没反应~我也想无刷新就变色~不用服务端代码