Moving PictureBox using Timer

假如想象 提交于 2021-02-10 20:15:19

问题


Basically what I'm trying to do is to make a picture box go up, then left, then down, then right, all based on timer ticks. I'm fairly new so I don't really know what's wrong. If you guys could give a simple answer or a better approach, that'd be great.

Dim slides As Integer

slides += 10
If slides < 20 Then
  PictureBox1.Left += 10
ElseIf slides > 20 AndAlso slides < 40 Then
  PictureBox1.Top += 10
ElseIf slides > 40 AndAlso < 60 Then
  PictureBox1.Left -= 10
ElseIf slides > 60 AndAlso < 80 Then
  PictureBox1.Top -= 10
Else
  slides = 0
End If

回答1:


Two things. Make sure your slides integer is outside the Tick event. Also, make sure to cover the condition of "equals", which your code doesn't check for, so slides is constantly falling into the "else" category and setting back to zero. That is, when slides equals 20, you don't have a condition that satisfies it, so it resets to zero.

Private slides As Integer

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  slides += 10
  If slides <= 20 Then
    PictureBox1.Left += 10
  ElseIf slides > 20 AndAlso slides <= 40 Then
    PictureBox1.Top += 10
  ElseIf slides > 40 AndAlso slides <= 60 Then
    PictureBox1.Left -= 10
  ElseIf slides > 60 AndAlso slides <= 80 Then
    PictureBox1.Top -= 10
  Else
    slides = 0
  End If
End Sub


来源:https://stackoverflow.com/questions/22666555/moving-picturebox-using-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!