按钮事件

zhuxiao17 2009-04-12 01:24:28
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;
}
如何可以一直点按钮一直循环~比如点一下红色,再点一下黄色,再点默认色,一直循环下去
...全文
183 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuxiao17 2009-04-12
  • 打赏
  • 举报
回复
C#可以的~JS脚本不行~按钮点上去没反应~我也想无刷新就变色~不用服务端代码
kkun_3yue3 2009-04-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhuxiao17 的回复:]
JS不行啊~昨天你给的都不行
[/Quote]

刚才的C#的也不行吗?
所有代码我都是调试通过后才回的贴
或许你应该把要求再说详细些
sunny906 2009-04-12
  • 打赏
  • 举报
回复
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;
}
hqq0326 2009-04-12
  • 打赏
  • 举报
回复
回帖是一种美德!
zhuxiao17 2009-04-12
  • 打赏
  • 举报
回复
JS还原默认色怎么写?
namhyuk 2009-04-12
  • 打赏
  • 举报
回复


zhuxiao17 2009-04-12
  • 打赏
  • 举报
回复
JS不行啊~昨天你给的都不行
zhuxiao17 2009-04-12
  • 打赏
  • 举报
回复
如果我想把颜色存入数据库里用什么方法比较好呢?
kkun_3yue3 2009-04-12
  • 打赏
  • 举报
回复
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的方式,这种页面上的逻辑交给客户端操作似乎更合理些!完全没必要上服务器上转个圈
LemIST 2009-04-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 LemIST 的回复:]
引用 2 楼 gentle_wolf 的回复:
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.…
[/Quote]
不好意思写错了
int currentIndex = colors.IndexOf(Button1.BackColor);
currentIndex = (currentIndex + 1) % colors.Length;
Button1.BackColor = colors[currentIndex];
LemIST 2009-04-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gentle_wolf 的回复:]
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 ha…
[/Quote]

int currentIndex = colors.IndexOf(Button1.BackColor);
currentIndex = (currentIndex + 1) / colors.Length;
Button1.BackColor = colors[currentIndex];
kkun_3yue3 2009-04-12
  • 打赏
  • 举报
回复
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;
}
}
}
}


TkingCN 2009-04-12
  • 打赏
  • 举报
回复
你想复杂了 我也遇见过一样的问题 不过其实代码很简单
if (this.Button1.BackColor == Color.Lime)
{
this.Button1.BackColor = Color.Red;

}else
{
this.Button1.BackColor == Color.Lime
}
cja03 2009-04-12
  • 打赏
  • 举报
回复
按照你的写法,把头尾接起来就行了:
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;
月影 2009-04-12
  • 打赏
  • 举报
回复
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.
kkun_3yue3 2009-04-12
  • 打赏
  • 举报
回复
帮顶,一会儿给贴代码

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧