在VS中新建一个Windows 窗体应用程序项目,然后从工具箱中拖拽两个按钮和一个文本框到Form1窗体中,分别双击两个按钮之后(其实就是建立按钮点击事件),在代码中粘贴如下代码即可
private void button1_Click(object sender, EventArgs e)
{
thistextBox1BackColor = ColorBlue;
thistextBox1TextAlign = HorizontalAlignmentCenter;
thistextBox1Text = "努力学习C#!";
}
private void button2_Click(object sender, EventArgs e)
{
thistextBox1BackColor = ColorWhite;
thistextBox1TextAlign = HorizontalAlignmentLeft;
thistextBox1ResetText();
}
控件如图,由左到右,由上到下,分别命名为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)
{
timer1Enabled = true;
}
if (onebreak==2)
{
timer2Enabled = true;
}
}
private void b2_Click(object sender, EventArgs e)
{
if (onebreak==0)
{
if (Isopen == false)
{
return;
}
b2Times += 1;
if (b2Times >= b3Times)
{
l1BackColor = ColorRed;
l2BackColor = ColorWhite;
}
}
}
private void b3_Click(object sender, EventArgs e)
{
if (onebreak==0)
{
if (Isopen == false)
{
return;
}
b3Times += 1;
if (b3Times >= b2Times)
{
l1BackColor = ColorWhite;
l2BackColor = ColorRed;
}
}
}
private void b4_Click(object sender, EventArgs e)
{
b3Times = 0;
b2Times = 0;
Isopen = false;
l1BackColor=ColorWhite;
l2BackColor = ColorWhite;
timer1Enabled = false;
timer2Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (onebreak == 1)
{
button1Text = "第一个灯泡坏掉";
onebreak = 0;
timer1Enabled = false;
button2Enabled = true;
return;
}
onebreak = 1;
l2BackColor = ColorWhite;
button1Text = "修好它";
button2Enabled = false;
if (Isopen==false)
{
return;
}
timer2Enabled = false;
timer1Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (onebreak == 2)
{
button2Text = "第二个灯泡坏掉";
onebreak = 0;
timer2Enabled = false;
button1Enabled = true;
return;
}
onebreak = 2;
l1BackColor = ColorWhite;
button2Text = "修好它";
button1Enabled = false;
if (Isopen == false)
{
return;
}
timer2Enabled = true;
timer1Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if ( l1BackColor != ColorRed)
{
l1BackColor = ColorRed;
return;
}
if (l1BackColor != ColorWhite)
{
l1BackColor = ColorWhite;
return;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (l2BackColor != ColorRed)
{
l2BackColor = ColorRed;
return;
}
if (l2BackColor != ColorWhite)
{
l2BackColor = ColorWhite;
return;
}
}
以上就是关于,大一的C#。要求编写一个WINDOWS窗体应用程序,窗体上有一个文本框和两个按钮(显示和清屏)全部的内容,包括:,大一的C#。要求编写一个WINDOWS窗体应用程序,窗体上有一个文本框和两个按钮(显示和清屏)、用C#语言编写windows窗体应用程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)