vb6编写的代码哪点出错了?
题目:用信号灯指挥交通,在窗体添加图片框p1和p2,分别代表信号灯和汽车,timer1控制信号灯的变化,timer2控制信号灯的变化,单击“开车”按钮,汽车向右移动,当绿...
题目:用信号灯指挥交通,在窗体添加图片框p1和p2,分别代表信号灯和汽车,timer1控制信号灯的变化,timer2控制信号灯的变化,单击“开车”按钮,汽车向右移动,当绿灯亮时车继续前进,红灯亮时停止前进,待绿灯亮时继续前进。
下面是我写的代码,请高手指正:
Private Sub Command1_Click()
Timer2.Enabled = true
End Sub
Private Sub Form_Load()
Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
Timer2.Enabled = false
End Sub
Private Sub Timer1_Timer()
If Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico") Then Image1.Picture = LoadPicture("H:\VB作业\红灯.ico")
If Image1.Picture = LoadPicture("H:\VB作业\红灯.ico") Then Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
End Sub
Private Sub Timer2_Timer()
If Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico") Then
Image2.Left = Image2.Left + 10
Else
Image2.Left = Image2.Left
End If
End Sub
其中:timer1的interval为2000,timer2的interval为10
我感到错误就在timer1中image1的picture设置上面,请哪位指教一下?直接把您编的代码写下来也行!谢谢! 展开
下面是我写的代码,请高手指正:
Private Sub Command1_Click()
Timer2.Enabled = true
End Sub
Private Sub Form_Load()
Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
Timer2.Enabled = false
End Sub
Private Sub Timer1_Timer()
If Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico") Then Image1.Picture = LoadPicture("H:\VB作业\红灯.ico")
If Image1.Picture = LoadPicture("H:\VB作业\红灯.ico") Then Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
End Sub
Private Sub Timer2_Timer()
If Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico") Then
Image2.Left = Image2.Left + 10
Else
Image2.Left = Image2.Left
End If
End Sub
其中:timer1的interval为2000,timer2的interval为10
我感到错误就在timer1中image1的picture设置上面,请哪位指教一下?直接把您编的代码写下来也行!谢谢! 展开
2个回答
展开全部
If Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico") Then
这个判断有问题,不可以这样,建议用一个标志变量判断
如
Dim flag As Boolean
Private Sub Command1_Click()
Timer2.Enabled = True
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Image1.Picture = LoadPicture("e:\white.gif")
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
If flag Then
Image1.Picture = LoadPicture("e:\black.gif")
Else
Image1.Picture = LoadPicture("e:\white.gif")
End If
flag = Not flag
End Sub
Private Sub Timer2_Timer()
If flag Then
Image2.Left = Image2.Left + 10
Else
Image2.Left = Image2.Left
End If
End Sub
这个判断有问题,不可以这样,建议用一个标志变量判断
如
Dim flag As Boolean
Private Sub Command1_Click()
Timer2.Enabled = True
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Image1.Picture = LoadPicture("e:\white.gif")
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
If flag Then
Image1.Picture = LoadPicture("e:\black.gif")
Else
Image1.Picture = LoadPicture("e:\white.gif")
End If
flag = Not flag
End Sub
Private Sub Timer2_Timer()
If flag Then
Image2.Left = Image2.Left + 10
Else
Image2.Left = Image2.Left
End If
End Sub
展开全部
'这么改:
Private Sub Command1_Click()
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub Form_Load()
Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
Static S
If S = 0 Then
Image1.Picture = LoadPicture("H:\VB作业\红灯.ico")
S = 1
Timer2.Enabled = False
Else
Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
S = 0
Timer2.Enabled = True
End If
End Sub
Private Sub Timer2_Timer()
Image2.Left = Image2.Left + 10
End Sub
Private Sub Command1_Click()
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub Form_Load()
Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
Timer1.Enabled = False
Timer2.Enabled = False
End Sub
Private Sub Timer1_Timer()
Static S
If S = 0 Then
Image1.Picture = LoadPicture("H:\VB作业\红灯.ico")
S = 1
Timer2.Enabled = False
Else
Image1.Picture = LoadPicture("H:\VB作业\绿灯.ico")
S = 0
Timer2.Enabled = True
End If
End Sub
Private Sub Timer2_Timer()
Image2.Left = Image2.Left + 10
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询