问题
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