用C#语言编写windows窗体应用程序
【例1】假设需要制造一个带有四个按钮和两个灯泡的盒子并具有以下功能:
⑴ 有四个按钮输入,分别称为B1,B2,B3和B4;
⑵ 有两个灯泡作为输出,分别称为L1和L2;
⑶ B1是打开电源的按钮;
⑷ B4是关闭电源的按钮;
⑸ B2和B3 是操作按钮;
⑹ 在B1被按下后及B4被按下前,系统应称为电源打开状态;
⑺ 在B4被按下后及B1被按下前,系统应称为电源关闭状态;
⑻ 在电源关闭状态下,B2和B3按钮不起作用;
⑼ 在电源关闭状态下,灯应不亮;
⑽ 从最近一次电源打开状态算起,如果B2被按下的次数比B3被按下的次数多,L1亮,否则L2亮。
⑾ 任何时候都不能有一个以上的灯泡亮;
⑿ 如果其中的一个灯泡出现故障,另一个灯泡应以2秒钟的间隔闪烁,而不管B2和B3的操作过程。当B4按下时,闪烁停止;当B1被按下时,闪烁重新开始。当故障被排除后闪烁停止,系统恢复正常状态。 展开
控件如图,由左到右,由上到下,分别命名为b1,b2,b3,b4,l1,l2,button1,button2,timer1,timer2
timer1与timer2的Enabled的属性默认为false,Interval默认为1000,
后代代码如下
bool Isopen = false;
int b2Times = 0;
int b3Times = 0;
int onebreak = 0;
private void b1_Click(object sender, EventArgs e)
{
Isopen = true;
if (onebreak==1)
{
timer1.Enabled = true;
}
if (onebreak==2)
{
timer2.Enabled = true;
}
}
private void b2_Click(object sender, EventArgs e)
{
if (onebreak==0)
{
if (Isopen == false)
{
return;
}
b2Times += 1;
if (b2Times >= b3Times)
{
l1.BackColor = Color.Red;
l2.BackColor = Color.White;
}
}
}
private void b3_Click(object sender, EventArgs e)
{
if (onebreak==0)
{
if (Isopen == false)
{
return;
}
b3Times += 1;
if (b3Times >= b2Times)
{
l1.BackColor = Color.White;
l2.BackColor = Color.Red;
}
}
}
private void b4_Click(object sender, EventArgs e)
{
b3Times = 0;
b2Times = 0;
Isopen = false;
l1.BackColor=Color.White;
l2.BackColor = Color.White;
timer1.Enabled = false;
timer2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (onebreak == 1)
{
button1.Text = "第一个灯泡坏掉";
onebreak = 0;
timer1.Enabled = false;
button2.Enabled = true;
return;
}
onebreak = 1;
l2.BackColor = Color.White;
button1.Text = "修好它";
button2.Enabled = false;
if (Isopen==false)
{
return;
}
timer2.Enabled = false;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (onebreak == 2)
{
button2.Text = "第二个灯泡坏掉";
onebreak = 0;
timer2.Enabled = false;
button1.Enabled = true;
return;
}
onebreak = 2;
l1.BackColor = Color.White;
button2.Text = "修好它";
button1.Enabled = false;
if (Isopen == false)
{
return;
}
timer2.Enabled = true;
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if ( l1.BackColor != Color.Red)
{
l1.BackColor = Color.Red;
return;
}
if (l1.BackColor != Color.White)
{
l1.BackColor = Color.White;
return;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (l2.BackColor != Color.Red)
{
l2.BackColor = Color.Red;
return;
}
if (l2.BackColor != Color.White)
{
l2.BackColor = Color.White;
return;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int GuZhang1=0,GuZhang2=0,L1=0,L2=0,B1=0,B4=0;
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)//断电状态,两灯均闭。
{
pictureBox1.Visible = false;
pictureBox2.Visible = false;
timer1.Enabled = false;
timer2.Enabled = false;
B4 = 1; B1 = 0;
}
private void button2_Click(object sender, EventArgs e)
{
if (B1 == 1)//灯1,在通电状态亮,且灯2灭
{
pictureBox1.Visible = true;
pictureBox2.Visible = false;
}
else
{
L1 += 1; //L1为灯1在断电以后使用的次数
}
if (L1 > 10) GuZhang1 = 1;//若在断电后按10次以上,则出现故障
}
private void button3_Click(object sender, EventArgs e)
{
if (B1 == 1)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
else
{
L2 += 1;
}
if (L2 > 10) GuZhang2 = 1;
}
private void button1_Click(object sender, EventArgs e)
{
if ((GuZhang1== 0)&&(GuZhang2==0))
{
B1 = 1; B4 = 0;
if (L1 > L2)
{
pictureBox1.Visible = true;
pictureBox2.Visible = false;
}
if (L2 > L1)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
}
else if (GuZhang1 == 0)
{
pictureBox1.Visible = false;
timer1.Enabled = true;
timer2.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (GuZhang1 == 1)
{
pictureBox1.Visible = false;
GuZhang2 = 0;
if (B4 != 0)
{
pictureBox2.Visible = true;
timer1.Enabled = false;
timer2.Enabled = true;
}
else
{
pictureBox2.Visible = false;
timer1.Enabled = false;
timer2.Enabled = false;
}
}
if (GuZhang2 == 1)
{
pictureBox2.Visible = false;
GuZhang1 = 0;
if (B4 != 0)
{
pictureBox1.Visible = true;
timer1.Enabled = false;
timer2.Enabled = true;
}
else
{
pictureBox1.Visible = false;
timer1.Enabled = false;
timer2.Enabled = false;
}
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (GuZhang1 == 1)
{
pictureBox1.Visible = false;
GuZhang2 = 0;
if (B4 != 0)
{
pictureBox2.Visible = false;
timer1.Enabled = true;
timer2.Enabled = false;
}
else
{
pictureBox1.Visible = false;
timer1.Enabled = false;
timer2.Enabled = false;
}
}
if (GuZhang2 == 1)
{
pictureBox2.Visible = false;
GuZhang1 = 0;
if (B4 != 0)
{
pictureBox1.Visible = false;
timer1.Enabled = true;
timer2.Enabled = false;
}
else
{
pictureBox1.Visible = false;
timer1.Enabled = false;
timer2.Enabled = false;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer2.Enabled = false;
this.timer2.Interval = 500;
}
}
}
开始把 B2 B3 按钮的 Eable 属性 设置为 false
B1 是打开电源
开始的时候按钮 B1 上文本是打开电源
打开后把 B1按钮上的值赋值为 关闭电源
在B1的按钮点击事件中写
B2.Eable=true;
B3.Eable=true;
你不是有2个 lable 控件嘛 L1 ,L2
默认 L1.Text="不亮";
L2.Text="不亮";
定义2个变量 来获取两个按钮分别的点击次数
然后 那个B1次数多就L1.Text ="亮";L2.text="不亮";
那个B2次数多就L2.Text ="亮";L1.Text ="不亮";
2010-12-19
只要你弄清楚了对象和事件就很简单了
这是一个简单的开关问题
只需要设两个bool值就行了
当a=true时B1是打的,当a=false的时候B1是关闭的
另两个也是一样的!要么执行要么不执行,正好适合用bool来做变量要么是true要么是false
然后搞清楚事件执行的先后就行了!!!
跟编程基本没什么关系。