VB程序红绿灯设计

2025-04-25 01:17:27
推荐回答(4个)
回答1:

1、  在桌面上,鼠标左键双击,打开VB编程软件,在左边工具栏上单击Image按钮,在Form1窗口上,绘制出图框,然后在Image属性面板的Picture属性上,单击【加载图片】按钮。

2、 接着,在弹出选取图片对话框,选择合适的红绿灯素材来体现效果,这里先选取了一张红灯的图片。

3、在Image属性面板的名称属性上,将其改名为red,标识后就不会与其他图混淆。

4、在VB编程的界面中也可以直接复制控件,如将red这个Image控件复制两个出来,不加入控件组,并将后两个依次改名为yellow、green。

5、 然后,将yellow图形控件与red控件重叠,并右键单击,在弹出的菜单中选择【移至底层】,放好后,green 图形控件放在最底层。三个图形控件依红、黄、绿的顺序叠加重合了。

6、接着,鼠标左键双击red图形控件,弹出输入代码窗口,在其上输入如下代码段。

回答2:


Dim r As Integer, y As Integer, g As Integer
Dim n As Integer, m As Integer
Private Sub Command1_Click(Index As Integer)
r = Val(Text1(0).Text)
y = Val(Text1(1).Text)
g = Val(Text1(2).Text)
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
Picture1(0).Picture = Form2.Picture1(0).Picture
Picture1(1).Picture = Form2.Picture1(1).Picture
Picture1(2).Picture = Form2.Picture1(2).Picture
Label2.Caption = 100
Label2.ForeColor = RGB(100, 100, 250)
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
End Sub

Private Sub Timer1_Timer()
m = m + 1
Picture1(0).Picture = Form2.Picture1(0).Picture
Picture1(1).Picture = LoadPicture("")
Picture1(2).Picture = LoadPicture("")
Label2.ForeColor = RGB(255, 0, 0)
Label2.Caption = r
If m Mod 5 = 0 Then
r = r - 1
If m > 500 Then m = 0
End If
If r < 0 Then
Timer1.Enabled = False
Timer2.Enabled = True
m = 0
End If
End Sub

Private Sub Timer2_Timer()
m = m + 1
Picture1(2).Picture = Form2.Picture1(2).Picture
Picture1(1).Picture = LoadPicture("")
Picture1(0).Picture = LoadPicture("")
Label2.ForeColor = RGB(0, 255, 0)
Label2.Caption = g
If m Mod 5 = 0 Then
g = g - 1
If m > 500 Then m = 0
End If
If g < 0 Then
Timer2.Enabled = False
Timer3.Enabled = True
m = 0
End If
End Sub

Private Sub Timer3_Timer()
m = m + 1
Picture1(1).Picture = Form2.Picture1(1).Picture
Picture1(0).Picture = LoadPicture("")
Picture1(2).Picture = LoadPicture("")
Label2.ForeColor = RGB(255, 255, 0)
Label2.Caption = y
If m Mod 5 = 0 Then
y = y - 1
Picture1(1).Picture = LoadPicture("")
If m > 500 Then m = 0
End If
If y < 0 Then
Timer3.Enabled = False
Timer1.Enabled = True
r = Val(Text1(0).Text)
y = Val(Text1(1).Text)
g = Val(Text1(2).Text)
m = 0
End If
End Sub

 

回答3:

Dim a, b, c As Integer   '事先把三个timer控件的interval设为1000,enabled属性设为false ,并且文本框只能输入整数 

Private Sub Command1_Click()   '开始按钮
Timer1 = True
Label4.Caption = Text1.Text
image1.Visible = True
image2.Visible = False
image3.Visible = False
a = Text1.Text
b = Text2.Text
c = Text3.Text
End Sub
Private Sub Command2_Click()    '结束按钮
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
image1.Visible = False
image2.Visible = False
image3.Visible = False
End Sub
Private Sub Timer1_Timer()
Text1.Text = Text1.Text - 1
Label4.Caption = Text1.Text
If Text1.Text = 0 Then
Text2.Text = b
Label4.Caption = b
Timer2.Enabled = True
Timer1.Enabled = False
image2.Visible = True
image1.Visible = False
End If
End Sub
Private Sub Timer2_Timer()
Text2.Text = Text2.Text - 1
Label4.Caption = Text2.Text
If Text2.Text = 0 Then
Text3.Text = c
Label4.Caption = c
Timer3.Enabled = True
Timer2.Enabled = False
image2.Visible = False
image3.Visible = True
End If
End Sub
Private Sub Timer3_Timer()
Text3.Text = Text3.Text - 1
Label4.Caption = Text3.Text
If Text3.Text = 0 Then
Text1.Text = a
Label4.Caption = a
Timer1.Enabled = True
Timer3.Enabled = False
image1.Visible = True
 
image3.Visible = False
End If
End Sub

回答4:

参考:http://zhidao.baidu.com/question/27904159