当前位置:编程学习 > VB >>

用VB.NET编写一个交通灯的程序

补充:代码是VB.NET编写的谢谢、

追问:最好晚上能发一个

答案:写一个与上面例子不同的,参考学习吧

加一个窗体,粘贴下面代码,运行即可

Public Class Form1
Dim cA() As Color = {Color.Lime, Color.Yellow, Color.Red}
Dim cB() As Color = {Color.DarkGreen, Color.DarkKhaki, Color.DarkRed}
Dim tmr() As Integer = {55, 5, 65}


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = Not Timer1.Enabled
Button1.Text = IIf(Timer1.Enabled, "停止", "继续")
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static A As Integer
Static T As Integer
If T = 0 Then

A += 1
If A > 2 Then A = 0
T = tmr(A) * 2
lblTime.ForeColor = cA(A)
lblLamp.ForeColor = cA(A)

Else
T -= 1

If T <= 10 Then
If T Mod 2 = 1 Then
lblTime.ForeColor = cA(A)
lblLamp.ForeColor = cA(A)
Else
lblTime.ForeColor = cB(A)
lblLamp.ForeColor = cB(A)
End If
End If
End If
lblTime.Text = (T \ 2).ToString
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Button1 = New System.Windows.Forms.Button
Me.Panel1 = New System.Windows.Forms.Panel
Me.lblTime = New System.Windows.Forms.Label
Me.lblLamp = New System.Windows.Forms.Label
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(298, 128)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(61, 28)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Panel1
'
Me.Panel1.BackColor = System.Drawing.Color.Black
Me.Panel1.Controls.Add(Me.lblTime)
Me.Panel1.Controls.Add(Me.lblLamp)
Me.Panel1.Location = New System.Drawing.Point(28, 56)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(134, 42)
Me.Panel1.TabIndex = 1
'
'lblTime
'
Me.lblTime.AutoSize = True
Me.lblTime.BackColor = System.Drawing.Color.Transparent
Me.lblTime.Font = New System.Drawing.Font("宋体", 20.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.lblTime.ForeColor = System.Drawing.Color.Red
Me.lblTime.Location = New System.Drawing.Point(87, 9)
Me.lblTime.Name = "lblTime"
Me.lblTime.Size = New System.Drawing.Size(26, 27)
Me.lblTime.TabIndex = 0
Me.lblTime.Text = "0"
'
'lblLamp
'
Me.lblLamp.AutoSize = True
Me.lblLamp.BackColor = System.Drawing.Color.Transparent
Me.lblLamp.Font = New System.Drawing.Font("宋体", 20.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.lblLamp.ForeColor = System.Drawing.Color.DarkKhaki
Me.lblLamp.Location = New System.Drawing.Point(16, 9)
Me.lblLamp.Name = "lblLamp"
Me.lblLamp.Size = New System.Drawing.Size(39, 27)
Me.lblLamp.TabIndex = 0
Me.lblLamp.Text = "●"
'
'Timer1
'
Me.Timer1.Interval = 500
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.Button1)
Me.Panel1.ResumeLayout(False)
Me.Panel1.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents lblTime As System.Windows.Forms.Label
Friend WithEvents lblLamp As System.Windows.Forms.Label
Friend WithEvents Timer1 As System.Windows.Forms.Timer


End Class

什么时候要呢,我家有,公司没开发环境

下面这个希望可以帮到你~

交通灯
实例说明

在本实例中,我们将制作一个具有交通灯效果的应用程序。程序运行后,交通灯不断地变化,可以通过按钮运行或停止程序。程序运行结果如图88-1所示。

技术要点

l 不断变换图片

l 不断更改时间间隔

< type="text/javascript"> < type="text/javascript" src=" http://pagead2.googlesyndication.com/pagead/show_ads.js">
实现过程

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"Traffic",然后选择保存路径。单击"确认"。

■ 添加控件

向当前窗体添加一个Timer控件,用于控制交通灯变换的时间;三个Picture控件,分别装入相应的图片;一个Button按钮,用于控制交通灯。属性保持默认值。

■ 添加代码

Dim vv As Boolean

Dim updirec As Boolean

Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click

vv = Timer1.Enabled

vv = Not vv

Timer1.Enabled = vv

If vv Then

Command1.Text = "开 始"

Else

Command1.Text = "停 止"

End If

End Sub

Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick

Static i As Short

Select Case i

Case 0

Timer1.Interval = 3000

picicon0.Visible = True

picicon1.Visible = False

picicon2.Visible = False

i = i + 1

updirec = False

Case 1

Timer1.Interval = 1000

picicon0.Visible = False

picicon1.Visible = True

picicon2.Visible = False

'Me.Icon = picicon(i).Image

If updirec Then

i = i - 1

Else

i = i + 1

End If

Case 2

Timer1.Interval = 3000

picicon0.Visible = False

picicon1.Visible = False

picicon2.Visible = True

i = i - 1

updirec = True

End Select

End Sub

■ 运行程序

单击菜单"调试|启动"或单击 图标运行程序。

上一个:请问各位大侠!VB怎么复习?
下一个:vb里的split()函数说明

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,